28 lines
697 B
JavaScript
28 lines
697 B
JavaScript
import { router } from '@/router';
|
|
import { useAuthStore } from '@/stores/auth';
|
|
import axios from 'axios';
|
|
|
|
export const http = axios.create({
|
|
baseURL: import.meta.env.VITE_API_BASE_URL,
|
|
});
|
|
|
|
http.interceptors.request.use((config) => {
|
|
const authStore = useAuthStore();
|
|
if (authStore.token) {
|
|
config.headers.Authorization = `Bearer ${authStore.token}`;
|
|
}
|
|
return config;
|
|
});
|
|
|
|
http.interceptors.response.use(
|
|
(response) => response,
|
|
(error) => {
|
|
if (error.response?.status === 401) {
|
|
const authStore = useAuthStore();
|
|
authStore.logout();
|
|
router.push('/login');
|
|
}
|
|
return Promise.reject(error);
|
|
}
|
|
);
|