misc_pterodactyl-panel/resources/scripts/api/interceptors.ts

23 lines
763 B
TypeScript
Raw Normal View History

2022-11-25 20:25:03 +00:00
import type { AxiosError } from 'axios';
import type { NavigateFunction } from 'react-router-dom';
import http from '@/api/http';
2022-11-25 20:25:03 +00:00
export const setupInterceptors = (navigate: NavigateFunction) => {
http.interceptors.response.use(
2022-11-25 20:25:03 +00:00
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')) {
2022-11-25 20:25:03 +00:00
navigate('/account', { state: { twoFactorRedirect: true } });
}
}
}
throw error;
2022-11-25 20:25:03 +00:00
},
);
};