[WEB-1564] chore: add interceptor to handle 401 errors and redirect users to login page. (#4788)

This commit is contained in:
Prateek Shourya 2024-06-12 19:18:48 +05:30 committed by GitHub
parent 4e5ca88e5e
commit 47dfb8797f
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 16 additions and 1 deletions

View File

@ -11,6 +11,20 @@ export abstract class APIService {
baseURL, baseURL,
withCredentials: true, withCredentials: true,
}); });
this.setupInterceptors();
}
private setupInterceptors() {
this.axiosInstance.interceptors.response.use(
(response) => response,
(error) => {
if (error.response && error.response.status === 401) {
window.location.reload();
}
return Promise.reject(error);
}
);
} }
get(url: string, params = {}, config = {}) { get(url: string, params = {}, config = {}) {

View File

@ -46,7 +46,8 @@ export class UserService extends APIService {
} }
async currentUser(): Promise<IUser> { async currentUser(): Promise<IUser> {
return this.get("/api/users/me/") // Using validateStatus: null to bypass interceptors for unauthorized errors.
return this.get("/api/users/me/", { validateStatus: null })
.then((response) => response?.data) .then((response) => response?.data)
.catch((error) => { .catch((error) => {
throw error?.response; throw error?.response;