e3da625927
- Backend Laravel 11 (API REST, JWT auth, PostgreSQL) - Frontend Next.js 14 (React, TypeScript, Tailwind CSS) - Docker Compose (PostgreSQL, backend, frontend) - Modules : auth, profil, tracker émotions, informations, admin
42 lines
1.0 KiB
TypeScript
42 lines
1.0 KiB
TypeScript
import axios from 'axios';
|
|
|
|
const api = axios.create({
|
|
baseURL: process.env.NEXT_PUBLIC_API_URL || 'http://localhost:8000/api/v1',
|
|
headers: {
|
|
'Content-Type': 'application/json',
|
|
'Accept': 'application/json',
|
|
},
|
|
});
|
|
|
|
// Intercepteur de requête : ajouter le token JWT si présent
|
|
api.interceptors.request.use(
|
|
(config) => {
|
|
if (typeof window !== 'undefined') {
|
|
const token = localStorage.getItem('cesizen_token');
|
|
if (token) {
|
|
config.headers.Authorization = `Bearer ${token}`;
|
|
}
|
|
}
|
|
return config;
|
|
},
|
|
(error) => Promise.reject(error)
|
|
);
|
|
|
|
// Intercepteur de réponse : gérer les erreurs 401
|
|
api.interceptors.response.use(
|
|
(response) => response,
|
|
(error) => {
|
|
if (
|
|
error.response?.status === 401 &&
|
|
typeof window !== 'undefined' &&
|
|
!window.location.pathname.includes('/connexion')
|
|
) {
|
|
localStorage.removeItem('cesizen_token');
|
|
window.location.href = '/connexion';
|
|
}
|
|
return Promise.reject(error);
|
|
}
|
|
);
|
|
|
|
export default api;
|