initial state
This commit is contained in:
@@ -0,0 +1,25 @@
|
||||
// src/lib/apiClient.ts
|
||||
import axios from 'axios';
|
||||
|
||||
export const api = axios.create({
|
||||
baseURL: '/api',
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
});
|
||||
|
||||
api.interceptors.request.use(config => {
|
||||
const token = localStorage.getItem('token');
|
||||
if (token) config.headers.Authorization = `Bearer ${token}`;
|
||||
return config;
|
||||
});
|
||||
|
||||
api.interceptors.response.use(
|
||||
res => res,
|
||||
async err => {
|
||||
if (err.response?.status === 401) {
|
||||
localStorage.removeItem('token');
|
||||
window.location.href = '/login';
|
||||
}
|
||||
// @ts-ignore
|
||||
return Promise.reject(err);
|
||||
}
|
||||
);
|
||||
Reference in New Issue
Block a user