import axios from 'axios'; import { API_CONFIG } from '../config/api'; import keycloak from '../lib/keycloak'; class ApiService { private api = axios.create({ baseURL: API_CONFIG.baseURL, timeout: API_CONFIG.timeout, headers: API_CONFIG.headers, }); constructor() { // Interceptor pour ajouter le token Keycloak this.api.interceptors.request.use( async (config) => { // Vérifier si Keycloak est initialisé et l'utilisateur authentifié if (keycloak && keycloak.authenticated) { try { // Rafraîchir le token si nécessaire (70 secondes avant expiration) await keycloak.updateToken(70); // Ajouter le token Bearer à l'en-tête Authorization if (keycloak.token) { config.headers['Authorization'] = `Bearer ${keycloak.token}`; } } catch (error) { console.error('Erreur lors de la mise à jour du token Keycloak:', error); // En cas d'erreur, rediriger vers la page de connexion keycloak.login(); throw error; } } return config; }, (error) => Promise.reject(error) ); // Interceptor pour les réponses this.api.interceptors.response.use( (response) => response, async (error) => { if (error.response?.status === 401) { // Essayer de rafraîchir le token Keycloak if (keycloak && keycloak.authenticated) { try { await keycloak.updateToken(-1); // Force refresh // Retry the original request return this.api.request(error.config); } catch (refreshError) { console.error('Impossible de rafraîchir le token:', refreshError); keycloak.login(); } } else { // Ne pas rediriger si on est en train de traiter un code d'autorisation if (typeof window !== 'undefined') { const currentUrl = window.location.href; const hasAuthCode = currentUrl.includes('code=') && currentUrl.includes('/dashboard'); if (!hasAuthCode) { console.log('❌ Non authentifié, redirection vers Keycloak...'); if (keycloak) { keycloak.login(); } } else { console.log('🔄 ApiService: Erreur 401 ignorée car authentification en cours...'); } } } } return Promise.reject(error); } ); } async get(url: string, config?: any): Promise { const response = await this.api.get(url, config); return response.data; } async post(url: string, data?: any, config?: any): Promise { const response = await this.api.post(url, data, config); return response.data; } async put(url: string, data?: any, config?: any): Promise { const response = await this.api.put(url, data, config); return response.data; } async delete(url: string, config?: any): Promise { const response = await this.api.delete(url, config); return response.data; } async patch(url: string, data?: any, config?: any): Promise { const response = await this.api.patch(url, data, config); return response.data; } } export default new ApiService();