mirror of
https://github.com/Tria-plc/edr-platform.git
synced 2026-08-27 16:40:56 +00:00
39 lines
978 B
TypeScript
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);
|
|
}
|
|
);
|