forked from Ding-Payments/ding-payments
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAuthGuard.tsx
More file actions
54 lines (43 loc) · 1.66 KB
/
Copy pathAuthGuard.tsx
File metadata and controls
54 lines (43 loc) · 1.66 KB
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
/**
* CLI-019 / CLI-042 — AuthGuard
*
* Route guard for protected tabs: auth state first, then local wallet readiness.
*
* State → redirect rules:
* - LOADING → render nothing (splash is shown by AnimatedSplashOverlay)
* - UNAUTHENTICATED → redirect to onboarding welcome
* - ONBOARDING → redirect to create-passkey
* - LOCKED → redirect to locked screen (re-auth)
* - READY → require walletStore hydrated + status ready + publicKey
*
* Loop safety: uses replace semantics; wallet-setup lives outside (tabs) guard.
*/
import { Redirect } from 'expo-router';
import { useWalletStore } from '@/features/wallet/state/walletStore';
import { useAuth } from '../hooks/useAuth';
export function AuthGuard({ children }: { children: React.ReactNode }) {
const { state } = useAuth();
const hasHydrated = useWalletStore((walletState) => walletState.hasHydrated);
const walletStatus = useWalletStore((walletState) => walletState.status);
const walletPublicKey = useWalletStore((walletState) => walletState.publicKey);
switch (state.status) {
case 'LOADING':
return null;
case 'UNAUTHENTICATED':
return <Redirect href="/(onboarding)/welcome" />;
case 'ONBOARDING':
return <Redirect href="/(onboarding)/create-passkey" />;
case 'LOCKED':
return <Redirect href="/(onboarding)/locked" />;
case 'READY':
if (!hasHydrated) {
return null;
}
if (walletStatus !== 'ready' || !walletPublicKey) {
return <Redirect href="/(onboarding)/wallet-setup" />;
}
return <>{children}</>;
default:
return <Redirect href="/(onboarding)/welcome" />;
}
}