This commit is contained in:
2026-08-20 09:10:36 +08:00
parent 033b5df6b6
commit 508f76924e
58 changed files with 1790 additions and 628 deletions
+18 -13
View File
@@ -61,6 +61,23 @@ export function registerAuthSessionExpiredHandler(handler: AuthSessionExpiredHan
authSessionExpiredHandler = handler
}
export async function refreshAuthenticatedSession(): Promise<string> {
if (!authRefreshHandler) throw new Error('认证刷新处理器尚未初始化')
if (!activeRefresh) {
activeRefresh = authRefreshHandler()
.catch(async (refreshError) => {
try {
await authSessionExpiredHandler?.()
} catch {
// 会话已清理,跳转失败不应覆盖原始认证错误。
}
throw refreshError
})
.finally(() => { activeRefresh = null })
}
return activeRefresh
}
http.interceptors.request.use((config) => {
const token = localStorage.getItem('cc-admin-token')
const isAnonymousAuthEndpoint = typeof config.url === 'string'
@@ -82,19 +99,7 @@ http.interceptors.response.use(
config._authRetry = true
try {
if (!activeRefresh) {
activeRefresh = authRefreshHandler()
.catch(async (refreshError) => {
try {
await authSessionExpiredHandler?.()
} catch {
// 会话已清理,跳转失败不应覆盖原始认证错误。
}
throw refreshError
})
.finally(() => { activeRefresh = null })
}
const nextToken = await activeRefresh
const nextToken = await refreshAuthenticatedSession()
config.headers.Authorization = `Bearer ${nextToken}`
return http.request(config)
} catch {