From 33f103d18501564b2cc840eedd30dc146c092e8f Mon Sep 17 00:00:00 2001 From: Ashesh <3626859+Ashesh3@users.noreply.github.com> Date: Fri, 20 Oct 2023 06:36:01 +0530 Subject: [PATCH] Fix session expiry detection (#6472) * fix session expiry detection * Fix logout bug --- src/Redux/fireRequest.tsx | 14 +++++++++----- src/Routers/AppRouter.tsx | 7 ++++++- 2 files changed, 15 insertions(+), 6 deletions(-) diff --git a/src/Redux/fireRequest.tsx b/src/Redux/fireRequest.tsx index bf083e23a92..3d8c677d47d 100644 --- a/src/Redux/fireRequest.tsx +++ b/src/Redux/fireRequest.tsx @@ -93,11 +93,15 @@ export const fireRequest = ( const config: any = { headers: {}, }; - if (!request.noAuth && localStorage.getItem(LocalStorageKeys.accessToken)) { - config.headers["Authorization"] = - "Bearer " + localStorage.getItem(LocalStorageKeys.accessToken); - } else { - // TODO: get access token + if (!request.noAuth) { + const access_token = localStorage.getItem(LocalStorageKeys.accessToken); + if (access_token) { + config.headers["Authorization"] = "Bearer " + access_token; + } else { + // The access token is missing from the local storage. Redirect to login page. + window.location.href = "/"; + return; + } } const axiosApiCall: any = axios.create(config); diff --git a/src/Routers/AppRouter.tsx b/src/Routers/AppRouter.tsx index 0f6108b00e3..d098a480149 100644 --- a/src/Routers/AppRouter.tsx +++ b/src/Routers/AppRouter.tsx @@ -65,7 +65,12 @@ export default function AppRouter() { useEffect(() => { addEventListener("storage", (event: any) => { - if (event.key === LocalStorageKeys.accessToken && !event.newValue) { + if ( + [LocalStorageKeys.accessToken, LocalStorageKeys.refreshToken].includes( + event.key + ) && + !event.newValue + ) { handleSignOut(true); } });