hfcron / src /lib /api.ts
Ajitdoval's picture
Update src/lib/api.ts
a08e1cb verified
Raw
History Blame Contribute Delete
875 Bytes
import axios from "axios";
import { getToken, removeToken } from "./auth";
// Pointing to /api so Next.js rewrites proxy it to the FastAPI backend on port 8000
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;