Spaces:
Configuration error
Configuration error
| import axios from "axios"; | |
| import { useAuthStore } from "@/store/authStore"; | |
| const API_BASE_URL = process.env.NEXT_PUBLIC_API_URL || "http://localhost:8000/api/v1"; | |
| const api = axios.create({ | |
| baseURL: API_BASE_URL, | |
| headers: { "Content-Type": "application/json" }, | |
| timeout: 15000, | |
| }); | |
| api.interceptors.request.use( | |
| (config) => { | |
| const token = useAuthStore.getState().token; | |
| if (token) { | |
| config.headers.Authorization = `Bearer ${token}`; | |
| } | |
| return config; | |
| }, | |
| (error) => Promise.reject(error) | |
| ); | |
| api.interceptors.response.use( | |
| (response) => response, | |
| (error) => { | |
| if (error.response?.status === 401) { | |
| useAuthStore.getState().logout(); | |
| if (typeof window !== "undefined") { | |
| window.location.href = "/login"; | |
| } | |
| } | |
| return Promise.reject(error); | |
| } | |
| ); | |
| export function getApiErrorMessage(err: unknown, fallback = "Something went wrong"): string { | |
| return (err as { response?: { data?: { detail?: string } } })?.response?.data?.detail || fallback; | |
| } | |
| export default api; | |