Files
edr-platform/apps/edr-freight-web/portal/src/utils/api.ts
2026-05-21 01:11:51 +03:00

39 lines
978 B
TypeScript

import {
UseQueryOptions,
QueryObserverOptions,
} from "@tanstack/react-query";
import axios from "axios";
// ---------------------------------------------------------------------------
// Axios client
// ---------------------------------------------------------------------------
export const client = axios.create({
baseURL: import.meta.env.VITE_API_URL,
});
// Attach auth token to every request
client.interceptors.request.use((config) => {
// TODO: replace with secure storage (cookie/localStorage/auth provider)
const token = document.cookie
.split("; ")
.find((row) => row.startsWith("auth-token="))
?.split("=")[1];
if (token) {
config.headers.Authorization = `Bearer ${token}`;
}
return config;
});
// Handle auth errors globally
client.interceptors.response.use(
(response) => response,
(error) => {
if (error.response?.status === 401) {
window.location.href = "/auth";
}
return Promise.reject(error);
}
);