init
This commit is contained in:
@@ -0,0 +1,39 @@
|
||||
import axios, { type AxiosError, HttpStatusCode } from 'axios';
|
||||
import { ElMessage } from 'element-plus';
|
||||
import router from '@/router';
|
||||
import { ROUTE_NAMES } from '@/consts/routeNames';
|
||||
import { useAuthStore } from '@/stores/auth';
|
||||
import { config } from '@/config';
|
||||
|
||||
export const api = axios.create({
|
||||
baseURL: config.api.baseUrl,
|
||||
withCredentials: true
|
||||
});
|
||||
|
||||
api.interceptors.response.use(
|
||||
response => response,
|
||||
(error: AxiosError) => {
|
||||
const status = error.response?.status;
|
||||
const url = error.config?.url ?? '';
|
||||
|
||||
if (status === HttpStatusCode.Unauthorized && !url.includes('/auth/login')) {
|
||||
ElMessage.error('Your session ended. Please sign in again.');
|
||||
|
||||
const { clearSession } = useAuthStore();
|
||||
|
||||
clearSession();
|
||||
|
||||
router.push({ name: ROUTE_NAMES.Login });
|
||||
}
|
||||
|
||||
if (status === HttpStatusCode.Forbidden) {
|
||||
ElMessage.error('You do not have access to this resource.');
|
||||
}
|
||||
|
||||
if (status === HttpStatusCode.TooManyRequests) {
|
||||
ElMessage.error('Too many requests. Please slow down and try again.');
|
||||
}
|
||||
|
||||
return Promise.reject(error);
|
||||
}
|
||||
);
|
||||
@@ -0,0 +1,6 @@
|
||||
import dayjs from 'dayjs';
|
||||
import relativeTime from 'dayjs/plugin/relativeTime';
|
||||
|
||||
dayjs.extend(relativeTime);
|
||||
|
||||
export default dayjs;
|
||||
Reference in New Issue
Block a user