feat: implement authentication flow and session management
- 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.
This commit is contained in:
+15
-2
@@ -1,5 +1,5 @@
|
||||
import React, { useEffect } from 'react';
|
||||
import { Stack } from 'expo-router';
|
||||
import { Stack, useRouter, useSegments } from 'expo-router';
|
||||
import { StatusBar } from 'expo-status-bar';
|
||||
import { useAuthStore } from '@store/authStore';
|
||||
|
||||
@@ -8,18 +8,31 @@ import { useAuthStore } from '@store/authStore';
|
||||
* Carrega a sessão armazenada e controla o fluxo auth vs app.
|
||||
*/
|
||||
export default function RootLayout(): React.ReactElement | null {
|
||||
const { loadStoredSession, isLoading } = useAuthStore();
|
||||
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>
|
||||
|
||||
Reference in New Issue
Block a user