- Refactor Profile and Login screens to use AuthContext for user data
- Enhance RequestPermitModal with multiple time-off types and validation - Implement CalendarWidget for visualizing time-off requests - Improve API error handling and token management - Add utility functions for consistent date and time formatting - Clean up unused mock data and update types
This commit is contained in:
@ -1,10 +1,8 @@
|
||||
import { createContext, PropsWithChildren, useContext, useEffect, useState } from 'react';
|
||||
import { SplashScreen, useRouter, useSegments } from 'expo-router';
|
||||
import AsyncStorage from '@react-native-async-storage/async-storage';
|
||||
import { UserData } from '@/types/types';
|
||||
import * as SecureStore from 'expo-secure-store';
|
||||
import api, { GATEWAY_ENDPOINT, GATEWAY_TOKEN } from './api';
|
||||
import axios from 'axios';
|
||||
import api, { KEY_TOKEN } from './api';
|
||||
|
||||
type AuthState = {
|
||||
isAuthenticated: boolean;
|
||||
@ -16,9 +14,6 @@ type AuthState = {
|
||||
|
||||
SplashScreen.preventAutoHideAsync();
|
||||
|
||||
const KEY_TOKEN = 'auth-token';
|
||||
const KEY_URL = 'App_URL';
|
||||
|
||||
export const AuthContext = createContext<AuthState>({
|
||||
isAuthenticated: false,
|
||||
isReady: false,
|
||||
@ -36,21 +31,12 @@ export function AuthProvider({ children }: PropsWithChildren) {
|
||||
const router = useRouter();
|
||||
const segments = useSegments();
|
||||
|
||||
const storeAuthState = async (newState: { isAuthenticated: boolean }) => {
|
||||
try {
|
||||
const jsonValue = JSON.stringify(newState);
|
||||
await AsyncStorage.setItem(KEY_TOKEN, jsonValue);
|
||||
} catch (error) {
|
||||
console.error('Errore nel salvataggio dello stato di autenticazione:', error);
|
||||
}
|
||||
}
|
||||
|
||||
const logIn = async (token: string, userData: UserData) => {
|
||||
try {
|
||||
await SecureStore.setItemAsync(KEY_TOKEN, token);
|
||||
setIsAuthenticated(true);
|
||||
setUser(userData);
|
||||
storeAuthState({ isAuthenticated: true }); // TODO: can be removed later
|
||||
|
||||
router.replace('/');
|
||||
} catch (error) {
|
||||
console.error('Errore durante il login:', error);
|
||||
@ -62,7 +48,7 @@ export function AuthProvider({ children }: PropsWithChildren) {
|
||||
await SecureStore.deleteItemAsync(KEY_TOKEN);
|
||||
setIsAuthenticated(false);
|
||||
setUser(null);
|
||||
storeAuthState({ isAuthenticated: false });
|
||||
|
||||
router.replace('/login');
|
||||
} catch (error) {
|
||||
console.error('Errore durante il logout:', error);
|
||||
@ -72,54 +58,45 @@ export function AuthProvider({ children }: PropsWithChildren) {
|
||||
useEffect(() => {
|
||||
const initApp = async () => {
|
||||
try {
|
||||
// 1. Gestione URL Gateway (Logica "else" del vecchio snippet)
|
||||
let currentApiUrl = await SecureStore.getItemAsync(KEY_URL);
|
||||
|
||||
if (!currentApiUrl) {
|
||||
console.log("URL non trovato, contatto Gateway...");
|
||||
try {
|
||||
// Chiamata diretta al gateway (senza interceptor api.ts)
|
||||
const gwResponse = await axios.get(GATEWAY_ENDPOINT, {
|
||||
headers: { "x-access-tokens": GATEWAY_TOKEN }
|
||||
});
|
||||
|
||||
// Supponiamo che il backend ritorni { url: "http://..." }
|
||||
// Adatta questo parsing alla risposta reale del tuo backend
|
||||
const newUrl = gwResponse.data.url + "/api/app_cantieri";
|
||||
|
||||
await SecureStore.setItemAsync(KEY_URL, newUrl);
|
||||
currentApiUrl = newUrl;
|
||||
console.log("URL acquisito:", newUrl);
|
||||
} catch (gwError) {
|
||||
console.error("Errore connessione Gateway:", gwError);
|
||||
// Qui potresti decidere di non bloccare l'app o mostrare un errore
|
||||
}
|
||||
}
|
||||
|
||||
// 2. Controllo Token e Recupero User (Logica "if" del vecchio snippet)
|
||||
// 1. Recupero Token salvato
|
||||
const savedToken = await SecureStore.getItemAsync(KEY_TOKEN);
|
||||
|
||||
if (savedToken && currentApiUrl) {
|
||||
// Verifichiamo il token chiamando /user
|
||||
// Qui usiamo l'istanza 'api' importata che ora userà l'URL e il token
|
||||
const userRes = await api.get("/user");
|
||||
if (savedToken) {
|
||||
console.log("Token trovato: ", savedToken);
|
||||
|
||||
const result = userRes.data;
|
||||
// 2. Chiamata al backend per verificare il token e scaricare i dati utente
|
||||
// Nota: api.ts aggiunge già l'header Authorization grazie all'interceptor (se configurato per leggere da SecureStore)
|
||||
// Se il tuo api.ts legge da AsyncStorage, assicurati che siano allineati, altrimenti passalo a mano qui:
|
||||
const response = await api.get("/user/info", {
|
||||
headers: { Authorization: `Bearer ${savedToken}` }
|
||||
});
|
||||
|
||||
const result = response.data;
|
||||
const userData = result.user;
|
||||
console.log("Sessione valida, dati utente caricati:", userData);
|
||||
|
||||
// 3. Mappatura dati (Backend -> Frontend)
|
||||
// Il backend actionMe ritorna: { id, username, role }
|
||||
const loadedUser: UserData = {
|
||||
name: result.nome,
|
||||
surname: result.cognome,
|
||||
username: result.username,
|
||||
email: result.email,
|
||||
role: result.role,
|
||||
id: result.id,
|
||||
id: userData.id,
|
||||
username: userData.username,
|
||||
role: userData.role,
|
||||
// Gestiamo i campi opzionali se il backend non li manda ancora
|
||||
name: userData.name,
|
||||
surname: userData.surname || '',
|
||||
email: userData.email || '',
|
||||
};
|
||||
|
||||
setUser(loadedUser);
|
||||
setIsAuthenticated(true);
|
||||
} else {
|
||||
console.log("Nessun token salvato.");
|
||||
}
|
||||
} catch (error) {
|
||||
console.error('Errore durante l\'inizializzazione dell\'app:', error);
|
||||
// Se il token è scaduto o l'API fallisce, consideriamo l'utente non loggato
|
||||
|
||||
} catch (error: any) {
|
||||
console.error('Errore inizializzazione (Token scaduto o Server down):', error.message);
|
||||
|
||||
// Se il token non è valido, puliamo tutto
|
||||
await SecureStore.deleteItemAsync(KEY_TOKEN);
|
||||
setIsAuthenticated(false);
|
||||
setUser(null);
|
||||
@ -129,32 +106,9 @@ export function AuthProvider({ children }: PropsWithChildren) {
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
// TODO: can be removed later
|
||||
// const getAuthFromStorage = async () => {
|
||||
// try {
|
||||
// const jsonValue = await AsyncStorage.getItem(KEY_TOKEN);
|
||||
// if (jsonValue != null) {
|
||||
// const auth = JSON.parse(jsonValue);
|
||||
// setIsAuthenticated(auth.isAuthenticated);
|
||||
// }
|
||||
// } catch (error) {
|
||||
// console.error('Errore nel recupero dello stato di autenticazione:', error);
|
||||
// }
|
||||
// setIsReady(true);
|
||||
// };
|
||||
// getAuthFromStorage();
|
||||
|
||||
initApp();
|
||||
}, []);
|
||||
|
||||
// TODO: Can be removed later
|
||||
// useEffect(() => {
|
||||
// if (isReady) {
|
||||
// SplashScreen.hideAsync();
|
||||
// }
|
||||
// }, [isReady]);
|
||||
|
||||
// Protezione rotte (opzionale, ma consigliata qui o nel Layout)
|
||||
useEffect(() => {
|
||||
if (!isReady) return;
|
||||
@ -164,7 +118,7 @@ export function AuthProvider({ children }: PropsWithChildren) {
|
||||
if (!isAuthenticated && inAuthGroup) {
|
||||
router.replace('/login');
|
||||
} else if (isAuthenticated && !inAuthGroup) {
|
||||
// router.replace('/(protected)/home'); // Decommenta se vuoi redirect automatico da login a home
|
||||
router.replace('/');
|
||||
}
|
||||
}, [isReady, isAuthenticated, segments]);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user