This commit is contained in:
2026-04-29 19:05:47 +08:00
parent b98c66ca21
commit 88c2fa5d03
40 changed files with 1781 additions and 2068 deletions
+30
View File
@@ -0,0 +1,30 @@
import axios from 'axios';
import { useAuthStore } from '../stores/useAuthStore';
const axiosInstance = axios.create({
baseURL: import.meta.env.VITE_API_BASE_URL ?? '/api',
timeout: 30_000,
});
axiosInstance.interceptors.request.use((config) => {
const { token } = useAuthStore.getState();
if (token) {
config.headers.Authorization = `Bearer ${token}`;
}
return config;
});
axiosInstance.interceptors.response.use(
(response) => response,
(error: unknown) => {
if (
axios.isAxiosError(error) &&
error.response?.status === 401
) {
useAuthStore.getState().logout();
}
return Promise.reject(error);
},
);
export default axiosInstance;