|
Last change
on this file was 9af201e, checked in by MBK <marija.karapandzova@…>, 12 days ago |
|
Fix frontend appearance
|
-
Property mode
set to
100644
|
|
File size:
653 bytes
|
| Line | |
|---|
| 1 |
|
|---|
| 2 | import React from 'react';
|
|---|
| 3 | import { Navigate } from 'react-router-dom';
|
|---|
| 4 |
|
|---|
| 5 | function ProtectedRoute({ children, requiredRoles = [] }) {
|
|---|
| 6 | const token = localStorage.getItem('token');
|
|---|
| 7 | const userStr = localStorage.getItem('user');
|
|---|
| 8 |
|
|---|
| 9 | // If no token, redirect to login
|
|---|
| 10 | if (!token) {
|
|---|
| 11 | return <Navigate to="/login" replace />;
|
|---|
| 12 | }
|
|---|
| 13 |
|
|---|
| 14 | // If roles are specified and user's role is not in the list, redirect to dashboard
|
|---|
| 15 | if (requiredRoles.length > 0 && userStr) {
|
|---|
| 16 | const user = JSON.parse(userStr);
|
|---|
| 17 | if (!requiredRoles.includes(user.role)) {
|
|---|
| 18 | return <Navigate to="/" replace />;
|
|---|
| 19 | }
|
|---|
| 20 | }
|
|---|
| 21 |
|
|---|
| 22 | return children;
|
|---|
| 23 | }
|
|---|
| 24 |
|
|---|
| 25 | export default ProtectedRoute;
|
|---|
Note:
See
TracBrowser
for help on using the repository browser.