2e96e07c7e
Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
58 lines
1.6 KiB
TypeScript
58 lines
1.6 KiB
TypeScript
import { useEffect } from 'react';
|
|
import { Stack, useRouter, useSegments } from 'expo-router';
|
|
import { StatusBar } from 'expo-status-bar';
|
|
import { QueryClient, QueryClientProvider } from '@tanstack/react-query';
|
|
import { useAuthStore } from '@/lib/auth-store';
|
|
import { View, ActivityIndicator } from 'react-native';
|
|
|
|
const queryClient = new QueryClient();
|
|
|
|
function AuthGuard({ children }: { children: React.ReactNode }) {
|
|
const { isAuthenticated, isLoading, loadToken } = useAuthStore();
|
|
const segments = useSegments();
|
|
const router = useRouter();
|
|
|
|
useEffect(() => {
|
|
loadToken();
|
|
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
}, []);
|
|
|
|
useEffect(() => {
|
|
if (isLoading) return;
|
|
|
|
const inAuthGroup = segments[0] === 'auth';
|
|
|
|
if (!isAuthenticated && !inAuthGroup) {
|
|
router.replace('/auth/login');
|
|
} else if (isAuthenticated && inAuthGroup) {
|
|
router.replace('/(tabs)');
|
|
}
|
|
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
}, [isAuthenticated, isLoading, segments]);
|
|
|
|
if (isLoading) {
|
|
return (
|
|
<View style={{ flex: 1, justifyContent: 'center', alignItems: 'center', backgroundColor: '#fff' }}>
|
|
<ActivityIndicator size="large" color="#000091" />
|
|
</View>
|
|
);
|
|
}
|
|
|
|
return <>{children}</>;
|
|
}
|
|
|
|
export default function RootLayout() {
|
|
return (
|
|
<QueryClientProvider client={queryClient}>
|
|
<AuthGuard>
|
|
<Stack screenOptions={{ headerShown: false }}>
|
|
<Stack.Screen name="auth" />
|
|
<Stack.Screen name="(tabs)" />
|
|
<Stack.Screen name="admin" />
|
|
</Stack>
|
|
</AuthGuard>
|
|
<StatusBar style="auto" />
|
|
</QueryClientProvider>
|
|
);
|
|
}
|