Spaces:
Configuration error
Configuration error
File size: 675 Bytes
244cb60 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | 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);
}
);
|