fix: Fix page logout issue due to refresh-token (#9970)

This commit is contained in:
Wu Tianwei 2024-10-29 09:55:14 +08:00 committed by GitHub
parent f47177ecb4
commit b6d045cebf
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -41,6 +41,7 @@ const useRefreshToken = () => {
return new Error('No access token or refresh token found')
}
if (localStorage?.getItem('is_refreshing') === '1') {
clearTimeout(timer.current)
timer.current = setTimeout(() => {
getNewAccessToken()
}, 1000)
@ -61,12 +62,14 @@ const useRefreshToken = () => {
localStorage?.setItem('console_token', access_token)
localStorage?.setItem('refresh_token', refresh_token)
const newTokenExpireTime = getExpireTime(access_token)
clearTimeout(timer.current)
timer.current = setTimeout(() => {
getNewAccessToken()
}, newTokenExpireTime - advanceTime.current - getCurrentTimeStamp())
}
else {
const newTokenExpireTime = getExpireTime(currentAccessToken)
clearTimeout(timer.current)
timer.current = setTimeout(() => {
getNewAccessToken()
}, newTokenExpireTime - advanceTime.current - getCurrentTimeStamp())
@ -74,8 +77,15 @@ const useRefreshToken = () => {
return null
}, [getExpireTime, getCurrentTimeStamp, handleError])
const handleVisibilityChange = useCallback(() => {
if (document.visibilityState === 'visible')
getNewAccessToken()
}, [])
useEffect(() => {
window.addEventListener('visibilitychange', handleVisibilityChange)
return () => {
window.removeEventListener('visibilitychange', handleVisibilityChange)
clearTimeout(timer.current)
localStorage?.removeItem('is_refreshing')
}