aboutsummaryrefslogtreecommitdiffstats
path: root/frontend/src/components/shared/ProtectedRoute.tsx
diff options
context:
space:
mode:
authorLibravatarLibravatar Biswa Kalyan Bhuyan <biswa@surgot.in> 2025-04-25 01:09:30 +0530
committerLibravatarLibravatar Biswa Kalyan Bhuyan <biswa@surgot.in> 2025-04-25 01:09:30 +0530
commit8733795c8449f3514369d7b4220934760e386f1b (patch)
tree86b70bd897f22646df401f5e24e28bb35f3ca155 /frontend/src/components/shared/ProtectedRoute.tsx
parent4cd8498ad30e6ea5f01e81346a81a2a1134864be (diff)
downloadfinance-8733795c8449f3514369d7b4220934760e386f1b.tar.gz
finance-8733795c8449f3514369d7b4220934760e386f1b.tar.bz2
finance-8733795c8449f3514369d7b4220934760e386f1b.zip
finance/frontend: fix: did some minor changes to the frontend
Diffstat (limited to 'frontend/src/components/shared/ProtectedRoute.tsx')
-rw-r--r--frontend/src/components/shared/ProtectedRoute.tsx37
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