File size: 1,806 Bytes
25f22bf |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 |
import axios from 'axios';
import cookieService from '../services/cookieService';
// Create axios instance with default config
const API_BASE_URL = import.meta.env.VITE_API_URL || 'http://localhost:5000';
const apiClient = axios.create({
baseURL: API_BASE_URL,
timeout: 30000,
headers: {
'Content-Type': 'application/json',
},
withCredentials: true, // Send cookies with requests
});
// Request interceptor to add auth token
apiClient.interceptors.request.use(
async (config) => {
// Get token from cookie service
const tokens = await cookieService.getAuthTokens();
if (tokens?.accessToken) {
config.headers.Authorization = `Bearer ${tokens.accessToken}`;
}
return config;
},
(error) => {
return Promise.reject(error);
}
);
// Response interceptor to handle token refresh
apiClient.interceptors.response.use(
(response) => {
return response;
},
async (error) => {
const originalRequest = error.config;
// If error is 401 and we haven't retried yet
if (error.response?.status === 401 && !originalRequest._retry) {
originalRequest._retry = true;
try {
// Attempt to refresh token
// In a real implementation, you would call a refresh endpoint
// For now, we'll just clear auth and redirect to login
await cookieService.clearAuthTokens();
localStorage.removeItem('token');
window.location.href = '/login';
} catch (refreshError) {
// If refresh fails, clear auth and redirect to login
await cookieService.clearAuthTokens();
localStorage.removeItem('token');
window.location.href = '/login';
return Promise.reject(refreshError);
}
}
return Promise.reject(error);
}
);
export default apiClient; |