diff options
Diffstat (limited to 'frontend/src/components/shared/ProtectedRoute.tsx')
-rw-r--r-- | frontend/src/components/shared/ProtectedRoute.tsx | 37 |
1 files changed, 37 insertions, 0 deletions
diff --git a/frontend/src/components/shared/ProtectedRoute.tsx b/frontend/src/components/shared/ProtectedRoute.tsx new file mode 100644 index 0000000..f5ecede --- /dev/null +++ b/frontend/src/components/shared/ProtectedRoute.tsx @@ -0,0 +1,37 @@ +'use client'; + +import { useEffect } from 'react'; +import { useRouter } from 'next/navigation'; +import { useAuth } from './AuthContext'; + +interface ProtectedRouteProps { + children: React.ReactNode; +} + +export default function ProtectedRoute({ children }: ProtectedRouteProps) { + const { isAuthenticated, isLoading } = useAuth(); + const router = useRouter(); + + useEffect(() => { + if (!isLoading && !isAuthenticated) { + router.push('/login'); + } + }, [isAuthenticated, isLoading, router]); + + // Show nothing while checking authentication + if (isLoading) { + return ( + <div className="flex items-center justify-center h-screen"> + <div className="w-8 h-8 border-4 border-primary border-t-transparent rounded-full animate-spin"></div> + </div> + ); + } + + // If not authenticated, don't render children (will redirect in useEffect) + if (!isAuthenticated) { + return null; + } + + // If authenticated, render children + return <>{children}</>; +}
\ No newline at end of file |