#28 feat(api): add logout.
This commit is contained in:
@@ -2,7 +2,7 @@ import axios from 'axios';
|
||||
import AuthService from '@/services/auth';
|
||||
|
||||
const http = axios.create({
|
||||
baseURL: import.meta.env.VITE_DJANGO_BASE_URL, // ← raíz del API
|
||||
baseURL: import.meta.env.VITE_DJANGO_BASE_URL,
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
},
|
||||
@@ -20,23 +20,19 @@ http.interceptors.request.use(
|
||||
);
|
||||
|
||||
http.interceptors.response.use(
|
||||
response => response, // paso directo si todo está OK
|
||||
response => response,
|
||||
async error => {
|
||||
const originalRequest = error.config;
|
||||
|
||||
// Sólo intentamos refrescar una vez
|
||||
if (error.response?.status === 401 && !originalRequest._retry) {
|
||||
originalRequest._retry = true;
|
||||
try {
|
||||
const newAccess = await AuthService.refresh(); // guarda el nuevo token
|
||||
// vuelve a colocar el header actualizado
|
||||
const newAccess = await AuthService.refresh();
|
||||
originalRequest.headers.Authorization = `Bearer ${newAccess}`;
|
||||
return http.request(originalRequest); // re‑intenta la petición
|
||||
return http.request(originalRequest);
|
||||
} catch (refreshError) {
|
||||
// Si el refresh falla, forzamos logout
|
||||
AuthService.logout();
|
||||
// opcional: redirigir al login
|
||||
window.location.href = '/login';
|
||||
window.location.href = '/autenticarse';
|
||||
return Promise.reject(refreshError);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user