forked from ZyntariHQ/Invoisio
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrequire-auth.tsx
More file actions
35 lines (28 loc) · 889 Bytes
/
Copy pathrequire-auth.tsx
File metadata and controls
35 lines (28 loc) · 889 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
34
35
'use client';
import { useEffect } from 'react';
import { useRouter } from 'next/navigation';
import { useWalletAuth } from '@/hooks/use-wallet-auth';
interface RequireAuthProps {
children: React.ReactNode;
redirectTo?: string;
}
export function RequireAuth({ children, redirectTo = '/login' }: RequireAuthProps) {
const { isAuthenticated, isLoading } = useWalletAuth();
const router = useRouter();
useEffect(() => {
if (!isLoading && !isAuthenticated) {
router.replace(redirectTo);
}
}, [isAuthenticated, isLoading, redirectTo, router]);
if (isLoading) {
return (
<div className="flex min-h-screen items-center justify-center bg-gray-50">
<div className="h-8 w-8 animate-spin rounded-full border-4 border-gray-300 border-t-blue-600" />
</div>
);
}
if (!isAuthenticated) {
return null;
}
return <>{children}</>;
}