Spaces:
Configuration error
Configuration error
| import axios from "axios"; | |
| const API_URL = process.env.NEXT_PUBLIC_API_URL || ""; | |
| export const api = axios.create({ | |
| baseURL: API_URL, | |
| headers: { "Content-Type": "application/json" }, | |
| }); | |
| api.interceptors.request.use((config) => { | |
| const token = typeof window !== "undefined" ? localStorage.getItem("token") : ""; | |
| if (token) config.headers.Authorization = `Bearer ${token}`; | |
| return config; | |
| }); | |
| api.interceptors.response.use( | |
| (response) => response, | |
| (error) => { | |
| if (error.response?.status === 401 && typeof window !== "undefined") { | |
| localStorage.removeItem("token"); | |
| window.location.href = "/login"; | |
| } | |
| return Promise.reject(error); | |
| } | |
| ); | |