From 8733795c8449f3514369d7b4220934760e386f1b Mon Sep 17 00:00:00 2001 From: Biswa Kalyan Bhuyan Date: Fri, 25 Apr 2025 01:09:30 +0530 Subject: finance/frontend: fix: did some minor changes to the frontend --- frontend/src/components/shared/ProtectedRoute.tsx | 37 +++++++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 frontend/src/components/shared/ProtectedRoute.tsx (limited to 'frontend/src/components/shared/ProtectedRoute.tsx') 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 ( +
+
+
+ ); + } + + // 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 -- cgit v1.2.3-59-g8ed1b