- Update RootLayout to handle authentication state and redirect users based on their auth status. - Create an Index component to redirect unauthenticated users to the login page. - Modify ApiAuthRepository to fetch user session data using access and refresh tokens. - Introduce a new container file to manage use case instances for authentication and filament operations. - Enhance authStore to validate tokens and fetch user data from the API. - Add babel-plugin-module-resolver for improved module imports. - Update package.json scripts for running the app on Android and iOS. - Add expo-camera dependency for camera functionalities. - Update tsconfig.json to include infrastructure path mapping.
42 lines
1.3 KiB
TypeScript
42 lines
1.3 KiB
TypeScript
import React, { useEffect } from 'react';
|
|
import { Stack, useRouter, useSegments } from 'expo-router';
|
|
import { StatusBar } from 'expo-status-bar';
|
|
import { useAuthStore } from '@store/authStore';
|
|
|
|
/**
|
|
* Root layout do expo-router.
|
|
* Carrega a sessão armazenada e controla o fluxo auth vs app.
|
|
*/
|
|
export default function RootLayout(): React.ReactElement | null {
|
|
const { loadStoredSession, isLoading, isAuthenticated } = useAuthStore();
|
|
const router = useRouter();
|
|
const segments = useSegments();
|
|
|
|
useEffect(() => {
|
|
loadStoredSession();
|
|
}, [loadStoredSession]);
|
|
|
|
useEffect(() => {
|
|
if (isLoading) return;
|
|
const inAuthGroup = segments[0] === '(auth)';
|
|
if (isAuthenticated && inAuthGroup) {
|
|
router.replace('/(app)/(tabs)/home');
|
|
} else if (!isAuthenticated && !inAuthGroup) {
|
|
router.replace('/(auth)/login');
|
|
}
|
|
}, [isAuthenticated, isLoading, segments, router]);
|
|
|
|
if (isLoading) return null;
|
|
|
|
return (
|
|
<>
|
|
<StatusBar style="light" backgroundColor="transparent" translucent />
|
|
<Stack screenOptions={{ headerShown: false, contentStyle: { backgroundColor: '#1E1B18' } }}>
|
|
<Stack.Screen name="index" />
|
|
<Stack.Screen name="(auth)" />
|
|
<Stack.Screen name="(app)" />
|
|
</Stack>
|
|
</>
|
|
);
|
|
}
|