misc_pterodactyl-panel/resources/scripts/api/interceptors.ts
2022-11-25 13:25:03 -07:00

22 lines
763 B
TypeScript

import type { AxiosError } from 'axios';
import type { NavigateFunction } from 'react-router-dom';
import http from '@/api/http';
export const setupInterceptors = (navigate: NavigateFunction) => {
http.interceptors.response.use(
resp => resp,
(error: AxiosError) => {
if (error.response?.status === 400) {
if (
(error.response?.data as Record<string, any>).errors?.[0].code === 'TwoFactorAuthRequiredException'
) {
if (!window.location.pathname.startsWith('/account')) {
navigate('/account', { state: { twoFactorRedirect: true } });
}
}
}
throw error;
},
);
};