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:
@@ -1,8 +1,11 @@
|
||||
import React from 'react';
|
||||
import { Tabs, Redirect } from 'expo-router';
|
||||
import React, { useEffect } from 'react';
|
||||
import { Tabs, Redirect, useRouter } from 'expo-router';
|
||||
import { View, TouchableOpacity, StyleSheet } from 'react-native';
|
||||
import { Ionicons } from '@expo/vector-icons';
|
||||
import { useAuthStore } from '@store/authStore';
|
||||
import { useFilamentStore } from '@store/filamentStore';
|
||||
import { usePresetStore } from '@store/presetStore';
|
||||
import { listFilamentsUseCase, listPresetsUseCase } from '@infrastructure/container';
|
||||
import { colors, radius, spacing } from '@shared/theme';
|
||||
|
||||
/**
|
||||
@@ -10,7 +13,27 @@ import { colors, radius, spacing } from '@shared/theme';
|
||||
* 4 abas + FAB central: Início | Estoque | [+] | Config | Perfil
|
||||
*/
|
||||
export default function AppLayout(): React.ReactElement {
|
||||
const { isAuthenticated } = useAuthStore();
|
||||
const { isAuthenticated, user } = useAuthStore();
|
||||
const router = useRouter();
|
||||
const { setFilaments, setLoading, setError } = useFilamentStore();
|
||||
const { setPresets } = usePresetStore();
|
||||
|
||||
useEffect(() => {
|
||||
if (!user) return;
|
||||
setLoading(true);
|
||||
Promise.all([
|
||||
listFilamentsUseCase.execute(user.id),
|
||||
listPresetsUseCase.execute(user.id),
|
||||
]).then(([filaments, presets]) => {
|
||||
setFilaments(filaments);
|
||||
setPresets(presets);
|
||||
}).catch((err) => {
|
||||
console.error('bootstrap error', err);
|
||||
setError('Não foi possível carregar os dados.');
|
||||
}).finally(() => {
|
||||
setLoading(false);
|
||||
});
|
||||
}, [user, setFilaments, setPresets, setLoading, setError]);
|
||||
|
||||
if (!isAuthenticated) {
|
||||
return <Redirect href="/(auth)/login" />;
|
||||
@@ -54,7 +77,12 @@ export default function AppLayout(): React.ReactElement {
|
||||
</View>
|
||||
),
|
||||
tabBarButton: (props) => (
|
||||
<TouchableOpacity {...props} style={styles.fabWrapper} activeOpacity={0.8} />
|
||||
<TouchableOpacity
|
||||
{...props}
|
||||
style={styles.fabWrapper}
|
||||
activeOpacity={0.8}
|
||||
onPress={() => router.push('/(app)/inventory/new')}
|
||||
/>
|
||||
),
|
||||
}}
|
||||
/>
|
||||
|
||||
@@ -1,17 +1,9 @@
|
||||
import React from 'react';
|
||||
import { useRouter } from 'expo-router';
|
||||
import { Screen } from '@presentation/components/layout/Screen';
|
||||
|
||||
/**
|
||||
* Tab "add" — apenas redireciona para o formulário de novo filamento.
|
||||
* O FAB central da tab bar chama esta rota.
|
||||
* Tab "add" — tela placeholder; a navegação é interceptada pelo FAB no _layout.
|
||||
*/
|
||||
export default function AddTab(): React.ReactElement {
|
||||
const router = useRouter();
|
||||
|
||||
React.useEffect(() => {
|
||||
router.replace('/(app)/inventory/new');
|
||||
}, [router]);
|
||||
|
||||
return <Screen />;
|
||||
}
|
||||
|
||||
@@ -56,7 +56,7 @@ export default function HomeScreen(): React.ReactElement {
|
||||
</View>
|
||||
<TouchableOpacity
|
||||
style={styles.qrBtn}
|
||||
onPress={() => router.push('/(app)/qrcode/scan')}
|
||||
onPress={() => router.push('/(app)/scanner' as never)}
|
||||
>
|
||||
<Ionicons name="qr-code-outline" size={22} color={colors.textPrimary} />
|
||||
</TouchableOpacity>
|
||||
|
||||
Reference in New Issue
Block a user