2022-11-25 20:25:03 +00:00
|
|
|
import type { AxiosError } from 'axios';
|
|
|
|
import type { NavigateFunction } from 'react-router-dom';
|
|
|
|
|
2020-12-06 21:56:14 +00:00
|
|
|
import http from '@/api/http';
|
|
|
|
|
2022-11-25 20:25:03 +00:00
|
|
|
export const setupInterceptors = (navigate: NavigateFunction) => {
|
2022-06-26 19:13:52 +00:00
|
|
|
http.interceptors.response.use(
|
2022-11-25 20:25:03 +00:00
|
|
|
resp => resp,
|
2022-06-26 19:13:52 +00:00
|
|
|
(error: AxiosError) => {
|
|
|
|
if (error.response?.status === 400) {
|
2022-06-26 21:31:47 +00:00
|
|
|
if (
|
|
|
|
(error.response?.data as Record<string, any>).errors?.[0].code === 'TwoFactorAuthRequiredException'
|
|
|
|
) {
|
2022-06-26 19:13:52 +00:00
|
|
|
if (!window.location.pathname.startsWith('/account')) {
|
2022-11-25 20:25:03 +00:00
|
|
|
navigate('/account', { state: { twoFactorRedirect: true } });
|
2022-06-26 19:13:52 +00:00
|
|
|
}
|
2020-12-06 21:56:14 +00:00
|
|
|
}
|
|
|
|
}
|
2022-06-26 19:13:52 +00:00
|
|
|
throw error;
|
2022-11-25 20:25:03 +00:00
|
|
|
},
|
2022-06-26 19:13:52 +00:00
|
|
|
);
|
2020-12-06 21:56:14 +00:00
|
|
|
};
|