refactored out of src packages

This commit is contained in:
2026-04-04 12:55:10 +05:30
parent 8a285bbdbe
commit 9871dac5c2
9 changed files with 0 additions and 0 deletions

38
react-auth/axios.ts Normal file
View File

@@ -0,0 +1,38 @@
import axios, { AxiosInstance } from "axios";
import { tokenStore } from "./token";
export function attachAuthInterceptors(client: AxiosInstance) {
client.interceptors.request.use((config) => {
const token = tokenStore.get();
if (token) {
if (!config.headers) {
(config as any).headers = {};
}
config.headers.Authorization = `Bearer ${token}`;
}
return config;
});
client.interceptors.response.use(
(res) => res,
(error) => {
if (error.response?.status === 401) {
tokenStore.clear();
}
return Promise.reject(error);
}
);
}
/**
* Factory for app APIs that need auth
*/
export function createApiClient(baseURL: string): AxiosInstance {
const client = axios.create({
baseURL,
headers: { "Content-Type": "application/json" },
});
attachAuthInterceptors(client);
return client;
}