mirror of
https://github.com/langgenius/dify.git
synced 2024-11-16 03:32:23 +08:00
fix: Page may lock if user close the page when refresh access_token (#10550)
This commit is contained in:
parent
e4d175780e
commit
40c5e6d67a
|
@ -1,11 +1,13 @@
|
||||||
import { apiPrefix } from '@/config'
|
import { apiPrefix } from '@/config'
|
||||||
import { fetchWithRetry } from '@/utils'
|
import { fetchWithRetry } from '@/utils'
|
||||||
|
|
||||||
|
const LOCAL_STORAGE_KEY = 'is_other_tab_refreshing'
|
||||||
|
|
||||||
let isRefreshing = false
|
let isRefreshing = false
|
||||||
function waitUntilTokenRefreshed() {
|
function waitUntilTokenRefreshed() {
|
||||||
return new Promise<void>((resolve, reject) => {
|
return new Promise<void>((resolve, reject) => {
|
||||||
function _check() {
|
function _check() {
|
||||||
const isRefreshingSign = localStorage.getItem('is_refreshing')
|
const isRefreshingSign = globalThis.localStorage.getItem(LOCAL_STORAGE_KEY)
|
||||||
if ((isRefreshingSign && isRefreshingSign === '1') || isRefreshing) {
|
if ((isRefreshingSign && isRefreshingSign === '1') || isRefreshing) {
|
||||||
setTimeout(() => {
|
setTimeout(() => {
|
||||||
_check()
|
_check()
|
||||||
|
@ -22,13 +24,14 @@ function waitUntilTokenRefreshed() {
|
||||||
// only one request can send
|
// only one request can send
|
||||||
async function getNewAccessToken(): Promise<void> {
|
async function getNewAccessToken(): Promise<void> {
|
||||||
try {
|
try {
|
||||||
const isRefreshingSign = localStorage.getItem('is_refreshing')
|
const isRefreshingSign = globalThis.localStorage.getItem(LOCAL_STORAGE_KEY)
|
||||||
if ((isRefreshingSign && isRefreshingSign === '1') || isRefreshing) {
|
if ((isRefreshingSign && isRefreshingSign === '1') || isRefreshing) {
|
||||||
await waitUntilTokenRefreshed()
|
await waitUntilTokenRefreshed()
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
globalThis.localStorage.setItem('is_refreshing', '1')
|
|
||||||
isRefreshing = true
|
isRefreshing = true
|
||||||
|
globalThis.localStorage.setItem(LOCAL_STORAGE_KEY, '1')
|
||||||
|
globalThis.addEventListener('beforeunload', releaseRefreshLock)
|
||||||
const refresh_token = globalThis.localStorage.getItem('refresh_token')
|
const refresh_token = globalThis.localStorage.getItem('refresh_token')
|
||||||
|
|
||||||
// Do not use baseFetch to refresh tokens.
|
// Do not use baseFetch to refresh tokens.
|
||||||
|
@ -61,15 +64,21 @@ async function getNewAccessToken(): Promise<void> {
|
||||||
return Promise.reject(error)
|
return Promise.reject(error)
|
||||||
}
|
}
|
||||||
finally {
|
finally {
|
||||||
|
releaseRefreshLock()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function releaseRefreshLock() {
|
||||||
|
if (isRefreshing) {
|
||||||
isRefreshing = false
|
isRefreshing = false
|
||||||
globalThis.localStorage.removeItem('is_refreshing')
|
globalThis.localStorage.removeItem(LOCAL_STORAGE_KEY)
|
||||||
|
globalThis.removeEventListener('beforeunload', releaseRefreshLock)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function refreshAccessTokenOrRelogin(timeout: number) {
|
export async function refreshAccessTokenOrRelogin(timeout: number) {
|
||||||
return Promise.race([new Promise<void>((resolve, reject) => setTimeout(() => {
|
return Promise.race([new Promise<void>((resolve, reject) => setTimeout(() => {
|
||||||
isRefreshing = false
|
releaseRefreshLock()
|
||||||
globalThis.localStorage.removeItem('is_refreshing')
|
|
||||||
reject(new Error('request timeout'))
|
reject(new Error('request timeout'))
|
||||||
}, timeout)), getNewAccessToken()])
|
}, timeout)), getNewAccessToken()])
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue
Block a user