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