Spaces:
Running on CPU Upgrade
Running on CPU Upgrade
Update frontend/src/components/ProtectedRoute.jsx
Browse files
frontend/src/components/ProtectedRoute.jsx
CHANGED
|
@@ -1,29 +1,48 @@
|
|
| 1 |
-
import React from 'react';
|
| 2 |
-
import { Navigate, Outlet } from 'react-router-dom';
|
| 3 |
-
import { useAuth } from './AuthContext';
|
| 4 |
-
|
| 5 |
-
export const ProtectedRoute = ({ requiredRole }) => {
|
| 6 |
-
const { token, loading, user } = useAuth();
|
| 7 |
-
|
| 8 |
-
if (loading) {
|
| 9 |
-
return (
|
| 10 |
-
<div className="min-h-screen flex flex-col items-center justify-center bg-surface-container-lowest font-body-md text-on-surface-variant">
|
| 11 |
-
<div className="flex flex-col items-center justify-center gap-lg animate-pulse">
|
| 12 |
-
<img src="/logo.png" alt="LarShield Logo" className="h-16 object-contain" />
|
| 13 |
-
<div className="flex items-center gap-sm">
|
| 14 |
-
<span className="material-symbols-outlined animate-spin text-[20px] text-primary">sync</span>
|
| 15 |
-
<span className="font-bold tracking-wide">Verifying LarShield Session...</span>
|
| 16 |
-
</div>
|
| 17 |
-
</div>
|
| 18 |
-
</div>
|
| 19 |
-
);
|
| 20 |
-
}
|
| 21 |
-
|
| 22 |
-
if (!token) return <Navigate to="/login" replace />;
|
| 23 |
-
|
| 24 |
-
if (requiredRole
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import React from 'react';
|
| 2 |
+
import { Navigate, Outlet } from 'react-router-dom';
|
| 3 |
+
import { useAuth } from './AuthContext';
|
| 4 |
+
|
| 5 |
+
export const ProtectedRoute = ({ requiredRole }) => {
|
| 6 |
+
const { token, loading, user } = useAuth();
|
| 7 |
+
|
| 8 |
+
if (loading) {
|
| 9 |
+
return (
|
| 10 |
+
<div className="min-h-screen flex flex-col items-center justify-center bg-surface-container-lowest font-body-md text-on-surface-variant">
|
| 11 |
+
<div className="flex flex-col items-center justify-center gap-lg animate-pulse">
|
| 12 |
+
<img src="/logo.png" alt="LarShield Logo" className="h-16 object-contain" />
|
| 13 |
+
<div className="flex items-center gap-sm">
|
| 14 |
+
<span className="material-symbols-outlined animate-spin text-[20px] text-primary">sync</span>
|
| 15 |
+
<span className="font-bold tracking-wide">Verifying LarShield Session...</span>
|
| 16 |
+
</div>
|
| 17 |
+
</div>
|
| 18 |
+
</div>
|
| 19 |
+
);
|
| 20 |
+
}
|
| 21 |
+
|
| 22 |
+
if (!token) return <Navigate to="/login" replace />;
|
| 23 |
+
|
| 24 |
+
if (requiredRole) {
|
| 25 |
+
const isMasterAuth = sessionStorage.getItem('superAdminAuth') === 'true';
|
| 26 |
+
const isImpersonating = !!localStorage.getItem('original_admin_token');
|
| 27 |
+
const role = user?.role;
|
| 28 |
+
|
| 29 |
+
let hasPermission = false;
|
| 30 |
+
if (Array.isArray(requiredRole)) {
|
| 31 |
+
hasPermission = requiredRole.includes(role) || role === 'super_admin' || isMasterAuth;
|
| 32 |
+
} else if (requiredRole === 'super_admin') {
|
| 33 |
+
hasPermission = role === 'super_admin' || isMasterAuth || isImpersonating;
|
| 34 |
+
} else if (requiredRole === 'admin') {
|
| 35 |
+
hasPermission = role === 'admin' || role === 'super_admin' || isMasterAuth || isImpersonating;
|
| 36 |
+
} else if (requiredRole === 'support_engineer') {
|
| 37 |
+
hasPermission = role === 'support_engineer' || role === 'admin' || role === 'super_admin' || isMasterAuth;
|
| 38 |
+
} else {
|
| 39 |
+
hasPermission = role === requiredRole || role === 'super_admin' || isMasterAuth;
|
| 40 |
+
}
|
| 41 |
+
|
| 42 |
+
if (!hasPermission) {
|
| 43 |
+
return <Navigate to="/dashboard" replace />;
|
| 44 |
+
}
|
| 45 |
+
}
|
| 46 |
+
|
| 47 |
+
return <Outlet />;
|
| 48 |
+
};
|