import axios from 'axios'; import { clearAuthSession, getAuthToken } from '../utils/authSession'; const configuredBaseUrl = import.meta.env.VITE_API_BASE_URL?.trim(); const baseURL = configuredBaseUrl || 'https://api.veriflo.app'; const api = axios.create({ baseURL, headers: { 'Content-Type': 'application/json', }, }); // Request interceptor to attach JWT token api.interceptors.request.use( (config) => { const token = getAuthToken(); if (token) { config.headers.Authorization = `Bearer ${token}`; } return config; }, (error) => { return Promise.reject(error); } ); // Response interceptor to handle global errors (e.g., Token expired) api.interceptors.response.use( (response) => { return response; }, (error) => { if (error.response && error.response.status === 401) { // Clear token and redirect to login if unauthorized clearAuthSession(); if (window.location.pathname !== '/login' && window.location.pathname !== '/signup') { window.location.href = '/login'; } } return Promise.reject(error); } ); export default api;