Skip to content

Commit

Permalink
Merge pull request #113 from suvarnakale/shiksha-2.0
Browse files Browse the repository at this point in the history
Issue #000 chore: refresh api loop issue fixed
  • Loading branch information
itsvick authored May 15, 2024
2 parents 58d476b + 3afedc0 commit f02f48d
Showing 1 changed file with 14 additions and 11 deletions.
25 changes: 14 additions & 11 deletions src/services/Interceptor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,6 @@ const refreshToken = async () => {
}
};



instance.interceptors.request.use(
(config) => {
if (typeof window !== 'undefined' && window.localStorage) {
Expand All @@ -48,17 +46,22 @@ instance.interceptors.response.use(
},
async (error) => {
const originalRequest = error.config;

if (error.response.data.statusCode === 401 && !originalRequest._retry) {
originalRequest._retry = true;
try {
const accessToken = await refreshToken();
originalRequest.headers.Authorization = `Bearer ${accessToken}`;
if (!accessToken) {
window.location.href = '/logout';
if (error?.response?.request?.responseURL.includes('/auth/refresh')) {
window.location.href = '/logout';
} else {
originalRequest._retry = true;
try {
const accessToken = await refreshToken();
originalRequest.headers.Authorization = `Bearer ${accessToken}`;
if (!accessToken) {
window.location.href = '/logout';
}
return instance(originalRequest);
} catch (refreshError) {
return Promise.reject(refreshError);
}
return instance(originalRequest);
} catch (refreshError) {
return Promise.reject(refreshError);
}
}
return Promise.reject(error);
Expand Down

0 comments on commit f02f48d

Please sign in to comment.