mirror of
https://github.com/KwiTsukasa/kt-template-online-playground.git
synced 2026-05-27 16:45:45 +08:00
fix(playground): 防止登录回跳死循环
This commit is contained in:
parent
49d9eb35e9
commit
0e271980c3
@ -15,6 +15,8 @@ type PersistedAuth = {
|
|||||||
const ACCESS_TOKEN_KEY = 'kt-admin-access-token'
|
const ACCESS_TOKEN_KEY = 'kt-admin-access-token'
|
||||||
const ACCESS_CODES_KEY = 'kt-admin-access-codes'
|
const ACCESS_CODES_KEY = 'kt-admin-access-codes'
|
||||||
const USER_INFO_KEY = 'kt-admin-user-info'
|
const USER_INFO_KEY = 'kt-admin-user-info'
|
||||||
|
const LOGIN_REDIRECT_MARK_KEY = 'kt-admin-login-redirect-at'
|
||||||
|
const LOGIN_REDIRECT_COOLDOWN = 10 * 1000
|
||||||
|
|
||||||
let refreshPromise: Promise<string | null> | null = null
|
let refreshPromise: Promise<string | null> | null = null
|
||||||
let redirectingToAdminLogin = false
|
let redirectingToAdminLogin = false
|
||||||
@ -46,6 +48,21 @@ export function clearPersistedAuth() {
|
|||||||
window.localStorage.removeItem(USER_INFO_KEY)
|
window.localStorage.removeItem(USER_INFO_KEY)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export function clearAdminLoginRedirectMark() {
|
||||||
|
window.sessionStorage.removeItem(LOGIN_REDIRECT_MARK_KEY)
|
||||||
|
}
|
||||||
|
|
||||||
|
export function shouldSkipRepeatedAdminLoginRedirect() {
|
||||||
|
const redirectAt = Number(
|
||||||
|
window.sessionStorage.getItem(LOGIN_REDIRECT_MARK_KEY),
|
||||||
|
)
|
||||||
|
|
||||||
|
return (
|
||||||
|
Number.isFinite(redirectAt) &&
|
||||||
|
Date.now() - redirectAt < LOGIN_REDIRECT_COOLDOWN
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
export function persistAuthData({
|
export function persistAuthData({
|
||||||
accessCodes,
|
accessCodes,
|
||||||
accessToken,
|
accessToken,
|
||||||
@ -80,6 +97,7 @@ export function redirectToAdminLogin() {
|
|||||||
if (redirectingToAdminLogin) return
|
if (redirectingToAdminLogin) return
|
||||||
|
|
||||||
redirectingToAdminLogin = true
|
redirectingToAdminLogin = true
|
||||||
|
window.sessionStorage.setItem(LOGIN_REDIRECT_MARK_KEY, String(Date.now()))
|
||||||
window.location.href = buildAdminLoginUrl(window.location.href)
|
window.location.href = buildAdminLoginUrl(window.location.href)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -1,9 +1,11 @@
|
|||||||
import axios, { type AxiosRequestConfig } from 'axios'
|
import axios, { type AxiosRequestConfig } from 'axios'
|
||||||
import {
|
import {
|
||||||
|
clearAdminLoginRedirectMark,
|
||||||
clearPersistedAuth,
|
clearPersistedAuth,
|
||||||
getStoredAccessToken,
|
getStoredAccessToken,
|
||||||
redirectToAdminLogin,
|
redirectToAdminLogin,
|
||||||
refreshPersistedAuth,
|
refreshPersistedAuth,
|
||||||
|
shouldSkipRepeatedAdminLoginRedirect,
|
||||||
} from './auth'
|
} from './auth'
|
||||||
|
|
||||||
export type ApiResponse<T = unknown> = {
|
export type ApiResponse<T = unknown> = {
|
||||||
@ -85,6 +87,7 @@ async function retryRequestWithFreshToken(config?: AuthRetryConfig) {
|
|||||||
|
|
||||||
function redirectAfterAuthExpired() {
|
function redirectAfterAuthExpired() {
|
||||||
clearPersistedAuth()
|
clearPersistedAuth()
|
||||||
|
if (shouldSkipRepeatedAdminLoginRedirect()) return
|
||||||
redirectToAdminLogin()
|
redirectToAdminLogin()
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -106,6 +109,7 @@ request.interceptors.response.use(
|
|||||||
return Promise.reject(new Error(data.msg || '请求失败'))
|
return Promise.reject(new Error(data.msg || '请求失败'))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
clearAdminLoginRedirectMark()
|
||||||
return data.data as any
|
return data.data as any
|
||||||
},
|
},
|
||||||
async (error) => {
|
async (error) => {
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user