- 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:
66
utils/api.ts
66
utils/api.ts
@ -1,43 +1,55 @@
|
||||
import axios from 'axios';
|
||||
import AsyncStorage from '@react-native-async-storage/async-storage';
|
||||
import * as SecureStore from 'expo-secure-store';
|
||||
|
||||
// CONFIGURAZIONE GATEWAY (Adatta questi valori al tuo DDEV)
|
||||
// Se sei su emulatore Android usa 10.0.2.2, se su iOS o fisico usa il tuo IP LAN (es 192.168.1.x)
|
||||
const GATEWAY_BASE_URL = "http://10.0.2.2:PORTA";
|
||||
export const GATEWAY_ENDPOINT = `${GATEWAY_BASE_URL}/tuo_endpoint_gateway`;
|
||||
export const GATEWAY_TOKEN = "il_tuo_token_statico_se_esiste";
|
||||
const API_BASE_URL = `http://10.0.2.2:32768/mobile`;
|
||||
export const KEY_TOKEN = 'auth_key';
|
||||
|
||||
// Crea un'istanza di axios
|
||||
const api = axios.create({
|
||||
|
||||
baseURL: API_BASE_URL,
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
'Accept': 'application/json',
|
||||
},
|
||||
timeout: 10000, // 10 secondi timeout
|
||||
});
|
||||
|
||||
// INTERCEPTOR: Configura ogni chiamata al volo
|
||||
api.interceptors.request.use(async (config) => {
|
||||
try {
|
||||
// 1. Cerchiamo se abbiamo già salvato l'URL finale del backend (post-gateway)
|
||||
const savedBaseUrl = await SecureStore.getItemAsync('App_URL');
|
||||
|
||||
if (savedBaseUrl) {
|
||||
config.baseURL = savedBaseUrl;
|
||||
} else {
|
||||
// Se non c'è, usiamo il gateway come fallback o gestiamo l'errore
|
||||
// (La logica di init nell'AuthContext dovrebbe averlo già settato)
|
||||
config.baseURL = GATEWAY_BASE_URL;
|
||||
}
|
||||
|
||||
// 2. Cerchiamo il token utente
|
||||
const token = await SecureStore.getItemAsync('auth-token');
|
||||
// Interceptor: Aggiunge il token a OGNI richiesta se esiste
|
||||
api.interceptors.request.use(
|
||||
async (config) => {
|
||||
const token = await SecureStore.getItemAsync(KEY_TOKEN);
|
||||
if (token) {
|
||||
// Adatta l'header in base al tuo backend (Bearer, x-access-tokens, etc.)
|
||||
config.headers.Authorization = `Bearer ${token}`;
|
||||
}
|
||||
} catch (error) {
|
||||
console.error("Errore interceptor:", error);
|
||||
console.log(`[API REQUEST] ${config.method?.toUpperCase()} ${config.url}`);
|
||||
return config;
|
||||
},
|
||||
(error) => {
|
||||
return Promise.reject(error);
|
||||
}
|
||||
return config;
|
||||
});
|
||||
);
|
||||
|
||||
// Interceptor: Gestione errori globale (es. token scaduto)
|
||||
api.interceptors.response.use(
|
||||
(response) => response,
|
||||
async (error) => {
|
||||
const originalRequest = error.config;
|
||||
|
||||
if (error.response) {
|
||||
console.error('[API ERROR]', error.response.status, error.response.data);
|
||||
|
||||
// Se riceviamo 401 (Unauthorized), potremmo voler fare il logout forzato
|
||||
if (error.response.status === 401) {
|
||||
// TODO: Qui potresti emettere un evento per disconnettere l'utente
|
||||
await SecureStore.deleteItemAsync(KEY_TOKEN);
|
||||
}
|
||||
} else {
|
||||
console.error('[API NETWORK ERROR]', error.message);
|
||||
}
|
||||
|
||||
return Promise.reject(error);
|
||||
}
|
||||
);
|
||||
|
||||
export default api;
|
||||
@ -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]);
|
||||
|
||||
|
||||
21
utils/dateTime.ts
Normal file
21
utils/dateTime.ts
Normal file
@ -0,0 +1,21 @@
|
||||
/**
|
||||
* Trasforma una data da "YYYY-MM-DD" a "DD/MM/YYYY"
|
||||
* @param dateStr stringa data in formato ISO "YYYY-MM-DD"
|
||||
* @returns stringa formattata "DD/MM/YYYY"
|
||||
*/
|
||||
export const formatDate = (dateStr: string | null | undefined): string => {
|
||||
if (!dateStr) return '';
|
||||
const [year, month, day] = dateStr.split('-');
|
||||
return `${day}/${month}/${year}`;
|
||||
};
|
||||
|
||||
/**
|
||||
* Trasforma un'ora da "HH:MM:SS" a "HH:MM"
|
||||
* @param timeStr stringa ora in formato "HH:MM:SS"
|
||||
* @returns stringa formattata "HH:MM"
|
||||
*/
|
||||
export const formatTime = (timeStr: string | null | undefined): string => {
|
||||
if (!timeStr) return '';
|
||||
const [hours, minutes] = timeStr.split(':');
|
||||
return `${hours}:${minutes}`;
|
||||
};
|
||||
Reference in New Issue
Block a user