25 lines
654 B
TypeScript
25 lines
654 B
TypeScript
import axios from "axios"
|
|
|
|
const API_URL = import.meta.env.VITE_API_URL || ""
|
|
|
|
const api = axios.create({
|
|
baseURL: `${API_URL}/api/v1`,
|
|
withCredentials: true, // Crucial for HttpOnly cookies
|
|
})
|
|
|
|
// Add a response interceptor to handle 401s
|
|
api.interceptors.response.use(
|
|
(response) => response,
|
|
(error) => {
|
|
if (error.response?.status === 401) {
|
|
// Unauthorized - session likely expired
|
|
// We can't use useNavigate here as it's not a React component
|
|
// But we can redirect to home which will trigger the login view in App.tsx
|
|
window.location.href = "/"
|
|
}
|
|
return Promise.reject(error)
|
|
}
|
|
)
|
|
|
|
export default api
|