| import axios from "axios"; |
| import { getToken, removeToken } from "./auth"; |
|
|
| |
| export const API_BASE_URL = "/api"; |
|
|
| const api = axios.create({ |
| baseURL: API_BASE_URL, |
| headers: { |
| "Content-Type": "application/json", |
| }, |
| }); |
|
|
| api.interceptors.request.use( |
| (config) => { |
| const token = getToken(); |
| if (token && config.headers) { |
| config.headers.Authorization = `Bearer ${token}`; |
| } |
| return config; |
| }, |
| (error) => Promise.reject(error) |
| ); |
|
|
| api.interceptors.response.use( |
| (response) => response, |
| (error) => { |
| if (error.response?.status === 401) { |
| removeToken(); |
| if (typeof window !== "undefined" && window.location.pathname !== "/") { |
| window.location.href = "/"; |
| } |
| } |
| return Promise.reject(error); |
| } |
| ); |
|
|
| export default api; |
|
|