fix(client): 修复登录会话自动续期

This commit is contained in:
2026-09-03 18:11:07 +08:00
parent 39e1c8e6ee
commit b3649c314a
9 changed files with 128 additions and 28 deletions
+7 -4
View File
@@ -46,6 +46,11 @@ function mapError(error: unknown) {
let refreshPromise: Promise<string> | null = null
let redirectingToLogin = false
const publicAuthPaths = ['/auth/login', '/auth/register', '/auth/refresh', '/auth/impersonations/exchange']
function isPublicAuthRequest(url?: string) {
return publicAuthPaths.some((path) => url?.endsWith(path))
}
function redirectToLoginAfterSessionExpiry() {
if (redirectingToLogin || typeof window === 'undefined') return
@@ -98,7 +103,7 @@ export async function refreshAuthenticatedSession() {
transport.interceptors.request.use((config) => {
const token = getAccessToken()
if (token) config.headers.Authorization = `Bearer ${token}`
if (token && !isPublicAuthRequest(config.url)) config.headers.Authorization = `Bearer ${token}`
return config
})
@@ -106,9 +111,7 @@ transport.interceptors.response.use(
(response) => response,
async (error: AxiosError) => {
const request = error.config as RetryableRequest | undefined
const publicAuthPaths = ['/auth/login', '/auth/register', '/auth/refresh', '/auth/impersonations/exchange']
const isPublicAuthRequest = publicAuthPaths.some((path) => request?.url?.endsWith(path))
if (error.response?.status !== 401 || !request || request._retriedAfterRefresh || isPublicAuthRequest) {
if (error.response?.status !== 401 || !request || request._retriedAfterRefresh || isPublicAuthRequest(request.url)) {
throw mapError(error)
}