DuOrigin v2 - React + NestJS + Prisma + EUDR API Integration
This commit is contained in:
31
frontend/src/components/ProtectedRoute.tsx
Normal file
31
frontend/src/components/ProtectedRoute.tsx
Normal file
@@ -0,0 +1,31 @@
|
||||
import { Navigate, useLocation } from 'react-router-dom';
|
||||
import { useAuth } from '@/hooks/useAuth';
|
||||
import { Loader2 } from 'lucide-react';
|
||||
|
||||
interface ProtectedRouteProps {
|
||||
children: React.ReactNode;
|
||||
adminOnly?: boolean;
|
||||
}
|
||||
|
||||
export default function ProtectedRoute({ children, adminOnly = false }: ProtectedRouteProps) {
|
||||
const { isAuthenticated, isLoading, user } = useAuth();
|
||||
const location = useLocation();
|
||||
|
||||
if (isLoading) {
|
||||
return (
|
||||
<div className="min-h-screen flex items-center justify-center bg-gray-bg">
|
||||
<Loader2 className="w-8 h-8 text-primary animate-spin" />
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
if (!isAuthenticated) {
|
||||
return <Navigate to="/login" state={{ from: location }} replace />;
|
||||
}
|
||||
|
||||
if (adminOnly && user?.role !== 'admin') {
|
||||
return <Navigate to="/dashboard" replace />;
|
||||
}
|
||||
|
||||
return <>{children}</>;
|
||||
}
|
||||
Reference in New Issue
Block a user