fix: prevent infinite redirect loops and pass error messages to logout handler in api interceptor
This commit is contained in:
parent
e7ac15250e
commit
96df79eaba
@ -54,11 +54,16 @@ Api.interceptors.response.use(
|
||||
}
|
||||
|
||||
const originalRequest = error.config as CustomAxiosRequestConfig;
|
||||
const errorDTO = error.response?.data as ErrorResponseDTO;
|
||||
|
||||
if (error.response?.status === 401 && !originalRequest._retry) {
|
||||
if (
|
||||
error.response?.status === 401 &&
|
||||
!originalRequest._retry &&
|
||||
originalRequest.url !== "/authentication"
|
||||
) {
|
||||
const userDataStr = localStorage.getItem("userData");
|
||||
if (!userDataStr) {
|
||||
handleLogout();
|
||||
handleLogout(errorDTO?.message);
|
||||
return Promise.reject(error);
|
||||
}
|
||||
|
||||
@ -119,7 +124,6 @@ Api.interceptors.response.use(
|
||||
}
|
||||
}
|
||||
|
||||
const errorDTO = error.response?.data as ErrorResponseDTO;
|
||||
if (errorDTO?.message) {
|
||||
toast.error(errorDTO.message);
|
||||
}
|
||||
@ -135,7 +139,9 @@ const handleLogout = (message?: string) => {
|
||||
toast.error(message);
|
||||
}
|
||||
|
||||
window.location.href = "/";
|
||||
if (window.location.pathname !== "/login") {
|
||||
window.location.href = "/login";
|
||||
}
|
||||
};
|
||||
|
||||
export const customInstance = <T>(
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user