43 lines
1.5 KiB
TypeScript
43 lines
1.5 KiB
TypeScript
import axios from 'axios';
|
|
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 = axios.create({
|
|
|
|
headers: {
|
|
'Content-Type': 'application/json',
|
|
},
|
|
});
|
|
|
|
// 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');
|
|
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);
|
|
}
|
|
return config;
|
|
});
|
|
|
|
export default api; |