| import axios from 'axios'; |
| import cookieService from '../services/cookieService'; |
|
|
| |
| const API_BASE_URL = import.meta.env.VITE_API_URL || |
| (import.meta.env.VITE_NODE_ENV === 'production' ? |
| 'http://46.62.218.169' : |
| 'http://localhost:5000'); |
| console.log('API_BASE_URL:', API_BASE_URL); |
|
|
| |
| const normalizedBaseUrl = API_BASE_URL.endsWith('/api') ? API_BASE_URL : `${API_BASE_URL}/api`; |
|
|
| const apiClient = axios.create({ |
| baseURL: normalizedBaseUrl, |
| timeout: 30000, |
| headers: { |
| 'Content-Type': 'application/json', |
| }, |
| withCredentials: true, |
| }); |
|
|
| |
| apiClient.interceptors.request.use( |
| async (config) => { |
| |
| const isDevelopment = import.meta.env.VITE_NODE_ENV === 'development'; |
| if (isDevelopment) { |
| console.log('π [API Request]', { |
| method: config.method?.toUpperCase(), |
| url: config.baseURL + config.url, |
| headers: config.headers, |
| data: config.data |
| }); |
|
|
| |
| if (config.url?.includes('/auth/register')) { |
| console.log('π [Auth Register] Request data verification:', { |
| hasConfirmPassword: 'confirmPassword' in (config.data || {}), |
| hasConfirmPassword: 'confirm_password' in (config.data || {}), |
| dataKeys: Object.keys(config.data || {}), |
| fullData: config.data |
| }); |
| } |
| } |
|
|
| |
| let token = null; |
| try { |
| const tokens = await cookieService.getAuthTokens(); |
| token = tokens?.accessToken; |
| } catch (error) { |
| console.warn('πͺ [Cookie] Error getting auth tokens, trying localStorage:', error.message); |
| token = localStorage.getItem('token'); |
| } |
|
|
| if (token) { |
| config.headers.Authorization = `Bearer ${token}`; |
| if (isDevelopment) { |
| console.log('π [Token] Added token to request headers'); |
| } |
| } else { |
| if (isDevelopment) { |
| console.log('π [Token] No token found for request'); |
| } |
| } |
|
|
| return config; |
| }, |
| (error) => { |
| console.error('β [API Request Error]', error); |
| return Promise.reject(error); |
| } |
| ); |
|
|
| |
| apiClient.interceptors.response.use( |
| (response) => { |
| |
| const isDevelopment = import.meta.env.VITE_NODE_ENV === 'development'; |
| if (isDevelopment) { |
| console.log('β
[API Response]', { |
| status: response.status, |
| method: response.config.method?.toUpperCase(), |
| url: response.config.baseURL + response.config.url, |
| data: response.data |
| }); |
| } |
| return response; |
| }, |
| async (error) => { |
| const isDevelopment = import.meta.env.VITE_NODE_ENV === 'development'; |
| const originalRequest = error.config; |
|
|
| |
| if (isDevelopment) { |
| console.error('β [API Response Error]', { |
| status: error.response?.status, |
| method: originalRequest?.method?.toUpperCase(), |
| url: originalRequest ? originalRequest.baseURL + originalRequest.url : 'unknown', |
| message: error.message, |
| response: error.response?.data, |
| headers: error.response?.headers |
| }); |
| } |
|
|
| |
| if (error.response?.status === 401) { |
| if (isDevelopment) { |
| console.log('π [Auth] 401 error detected, attempting token refresh'); |
| } |
|
|
| |
| if (!originalRequest._retry) { |
| originalRequest._retry = true; |
|
|
| try { |
| |
| await cookieService.clearAuthTokens(); |
| localStorage.removeItem('token'); |
|
|
| if (isDevelopment) { |
| console.log('π [Auth] Cleared all authentication data'); |
| } |
|
|
| |
| const currentPath = window.location.pathname; |
| if (currentPath !== '/login' && currentPath !== '/register') { |
| if (isDevelopment) { |
| console.log('π [Auth] Redirecting to login page'); |
| } |
| window.location.href = '/login'; |
| } |
|
|
| } catch (refreshError) { |
| if (isDevelopment) { |
| console.error('π [Auth] Error during token refresh:', refreshError); |
| } |
|
|
| |
| await cookieService.clearAuthTokens(); |
| localStorage.removeItem('token'); |
| window.location.href = '/login'; |
|
|
| return Promise.reject(refreshError); |
| } |
| } |
| } |
|
|
| |
| if (!error.response) { |
| if (isDevelopment) { |
| console.error('π [Network] No response received:', { |
| url: originalRequest?.baseURL + originalRequest?.url, |
| message: error.message |
| }); |
| } |
|
|
| |
| |
| return Promise.reject(error); |
| } |
|
|
| return Promise.reject(error); |
| } |
| ); |
|
|
| export default apiClient; |