import { useEffect } from 'react'; import { Stack } from 'expo-router'; import { useAuthStore } from '@/store/auth'; import { useWebSocket } from '@/hooks/useWebSocket'; import { authClient } from '@/lib/auth-client'; /** * Root layout with auth guard * Routes to auth stack if not authenticated, app stack otherwise */ export default function RootLayout() { const { user, setUser, setIsLoading } = useAuthStore(); // Initialize WebSocket connection when authenticated useWebSocket(); useEffect(() => { // Check if user is already authenticated async function checkAuth() { try { setIsLoading(true); const response = await authClient.getSession(); const sessionData = (response as any)?.data; if (sessionData && (sessionData.user || sessionData.data?.user)) { const user = sessionData.user || sessionData.data?.user; setUser(user); } } catch (error) { console.error('Auth check failed:', error); } finally { setIsLoading(false); } } checkAuth(); }, [setUser, setIsLoading]); return ( {user ? ( // User is authenticated - show app stack ) : ( // User is not authenticated - show auth stack )} ); }