Something went wrong. Try again.
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051import "@/lib/polyfills";import "react-native-url-polyfill/auto";import "../global.css";import { useEffect } from "react";import { Stack, useRouter, useSegments } from "expo-router";import { useAuthStore } from "@/stores/auth";import { ActivityIndicator, View } from "react-native";
function AuthGate({ children }: { children: React.ReactNode }) { const { agent, isLoading, restore } = useAuthStore(); const segments = useSegments(); const router = useRouter();
useEffect(() => { restore(); }, []);
useEffect(() => { if (isLoading) return;
const onLoginScreen = segments[0] === "login";
if (!agent && !onLoginScreen) { router.replace("/login"); } else if (agent && onLoginScreen) { router.replace("/"); } }, [agent, isLoading, segments]);
if (isLoading) { return ( <View className="flex-1 items-center justify-center bg-white"> <ActivityIndicator size="large" /> </View> ); }
return <>{children}</>;}
export default function RootLayout() { return ( <AuthGate> <Stack screenOptions={{ headerShown: false }}> <Stack.Screen name="login" options={{ gestureEnabled: false }} /> <Stack.Screen name="(tabs)" /> </Stack> </AuthGate> );}