-
Notifications
You must be signed in to change notification settings - Fork 82
Expand file tree
/
Copy pathAuthGuard.tsx
More file actions
33 lines (27 loc) · 883 Bytes
/
Copy pathAuthGuard.tsx
File metadata and controls
33 lines (27 loc) · 883 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
'use client';
import { useEffect } from 'react';
import { useRouter, usePathname } from 'next/navigation';
import { useAuth } from '@/hooks/useAuth';
import { Loader2 } from 'lucide-react';
export function AuthGuard({ children }: { children: React.ReactNode }) {
const { isAuthenticated, isLoading } = useAuth();
const router = useRouter();
const pathname = usePathname();
useEffect(() => {
if (!isLoading && !isAuthenticated) {
// Fallback client-side redirect
router.push(`/?callbackUrl=${encodeURIComponent(pathname)}`);
}
}, [isLoading, isAuthenticated, router, pathname]);
if (isLoading) {
return (
<div className="flex h-screen w-full items-center justify-center">
<Loader2 className="h-8 w-8 animate-spin text-primary" />
</div>
);
}
if (!isAuthenticated) {
return null;
}
return <>{children}</>;
}