From 35cda75c962f15bd1c9810c45908a9f7b112cc5d Mon Sep 17 00:00:00 2001 From: "cyril.tsui" Date: Thu, 26 Oct 2023 14:24:58 +0800 Subject: [PATCH] Update JWT timeout --- src/auth/index.js | 31 ++++++++++++++----- .../auth-forms/AuthLoginCustom.js | 1 + 2 files changed, 25 insertions(+), 7 deletions(-) diff --git a/src/auth/index.js b/src/auth/index.js index e59189f..f2a12a6 100644 --- a/src/auth/index.js +++ b/src/auth/index.js @@ -93,6 +93,7 @@ export const handleLogoutFunction = () => { localStorage.removeItem(windowCount) localStorage.removeItem(predictProductionQty) localStorage.removeItem(predictUsageCount) + setTimeout(()=> localStorage.removeItem("expiredAlertShown"), 5000) } } @@ -109,11 +110,14 @@ export const SetupAxiosInterceptors = () => { // ** If token is present add it to request's Authorization Header if (isUserLoggedIn()) { config.headers.Authorization = `${jwtApplicationConfig.tokenType} ${accessToken}` + config.data = config.data === undefined ? JSON.stringify({refreshToken:null}) : config.data } config.headers['X-Authorization'] = process.env.REACT_APP_API_KEY return config }, - error => Promise.reject(error) + error => { + Promise.reject(error) + } ) axios.interceptors.response.use( @@ -124,23 +128,36 @@ export const SetupAxiosInterceptors = () => { error => { // ** const { config, response: { status } } = error const {response} = error - if (error.response.status === 401) { - dispatch(handleLogoutFunction()); - navigate('/login'); + if (localStorage.getItem("expiredAlertShown") === null) { + localStorage.setItem("expiredAlertShown", true) + alert("登入驗證已過期,請重新登入。") + } + } // ** if (status === 401) { if (response.status === 401) { - dispatch(handleLogoutFunction()); - navigate('/login'); + if (localStorage.getItem("expiredAlertShown") === null) { + localStorage.setItem("expiredAlertShown", true) + alert("登入驗證已過期,請重新登入。") + } } if (response && response.status === 401) { + if (localStorage.getItem("expiredAlertShown") === null) { + localStorage.setItem("expiredAlertShown", true) + alert("登入驗證已過期,請重新登入。") + } + } + + if (localStorage.getItem("expiredAlertShown")) { dispatch(handleLogoutFunction()); navigate('/login'); } + return Promise.reject(error) - } + }, + ) } diff --git a/src/pages/authentication/auth-forms/AuthLoginCustom.js b/src/pages/authentication/auth-forms/AuthLoginCustom.js index d2f9f6a..637f9e0 100644 --- a/src/pages/authentication/auth-forms/AuthLoginCustom.js +++ b/src/pages/authentication/auth-forms/AuthLoginCustom.js @@ -86,6 +86,7 @@ const AuthLoginCustom = () => { // setSuccess(true) dispatch(handleLogin(data)) navigate('/dashboard'); + location.reload() // setSumitting(false) }) .catch((error) => {