-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProtectedRoute.tsx
More file actions
37 lines (30 loc) · 923 Bytes
/
ProtectedRoute.tsx
File metadata and controls
37 lines (30 loc) · 923 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
import { ReactNode } from 'react';
import { Navigate } from 'react-router-dom';
import { useAuth } from '../hooks/useAuth';
/**
* Protected Route Component
*
* Restricts access to authenticated users
* Redirects to login if not authenticated
*/
interface ProtectedRouteProps {
children: ReactNode;
requiredRole?: 'MANAGER' | 'MEMBER';
}
export function ProtectedRoute({ children, requiredRole }: ProtectedRouteProps) {
const { isAuthenticated, user, isLoading } = useAuth();
if (isLoading) {
return (
<div className="flex items-center justify-center min-h-screen">
<div className="animate-spin rounded-full h-12 w-12 border-b-2 border-blue-600" />
</div>
);
}
if (!isAuthenticated) {
return <Navigate to="/login" replace />;
}
if (requiredRole && user?.role !== requiredRole) {
return <Navigate to="/unauthorized" replace />;
}
return <>{children}</>;
}