From 3afedc0593dec74bf52d208867953378735d6fc7 Mon Sep 17 00:00:00 2001 From: suvarnakale Date: Wed, 15 May 2024 10:04:03 +0530 Subject: [PATCH] Issue #000 chore: refresh api loop issue fixed --- src/services/Interceptor.ts | 25 ++++++++++++++----------- 1 file changed, 14 insertions(+), 11 deletions(-) diff --git a/src/services/Interceptor.ts b/src/services/Interceptor.ts index 6d370947..0a94c374 100644 --- a/src/services/Interceptor.ts +++ b/src/services/Interceptor.ts @@ -22,8 +22,6 @@ const refreshToken = async () => { } }; - - instance.interceptors.request.use( (config) => { if (typeof window !== 'undefined' && window.localStorage) { @@ -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);