import axios from 'axios'; // Create a configured Axios instance const api = axios.create({ baseURL: process.env.NEXT_PUBLIC_API_BASE_URL || 'http://localhost:8080/api/v1', headers: { 'Content-Type': 'application/json', }, }); // Request interceptor for adding the auth token api.interceptors.request.use( (config) => { // Only access localStorage if we are running in the browser if (typeof window !== 'undefined') { // const token = localStorage.getItem('token'); const token = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJmb3J0dW5lLWJldCIsImF1ZCI6WyJhcGkuZm9ydHVuZWJldHMubmV0Il0sImV4cCI6MTc3MjI3NzQxNSwibmJmIjoxNzcyMjc2ODE1LCJpYXQiOjE3NzIyNzY4MTUsIlVzZXJJZCI6NCwiUm9sZSI6InN1cGVyX2FkbWluIiwiQ29tcGFueUlEIjp7IlZhbHVlIjowLCJWYWxpZCI6ZmFsc2V9fQ.QJJ1KAFkWWCMmxxBi8rQc9C5aChN2XmTys-RCufV_Zo"; if (token) { config.headers.Authorization = `Bearer ${token}`; } } return config; }, (error) => { return Promise.reject(error); } ); // Response interceptor for handling common errors (like 401 Unauthorized) api.interceptors.response.use( (response) => { return response; }, (error) => { if (error.response?.status === 401) { // Handle unauthorized errors, e.g., redirecting to login or clearing the token if (typeof window !== 'undefined') { localStorage.removeItem('token'); // Uncomment the line below to redirect automatically // window.location.href = '/login'; } } return Promise.reject(error); } ); export default api;