From 787ad284243257ce7cf3c71db487a44de0761cf2 Mon Sep 17 00:00:00 2001 From: Zyy Date: Wed, 14 Oct 2026 22:00:53 +0800 Subject: [PATCH] fix(auth): show login when initial session restore fails Redirect an initial navigation to login when the status request fails, while preserving an already open page and its session on later failures. Co-authored-by: Codex --- frontend/src/router/index.ts | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/frontend/src/router/index.ts b/frontend/src/router/index.ts index ef0d6666a..bc9278cf6 100644 --- a/frontend/src/router/index.ts +++ b/frontend/src/router/index.ts @@ -1,5 +1,5 @@ import type { AuthSession } from '@/api' -import { createRouter, createWebHistory } from 'vue-router' +import { createRouter, createWebHistory, START_LOCATION } from 'vue-router' import { useAuthStore } from '@/stores/modules/auth' import { routes } from './routes' @@ -16,7 +16,7 @@ function resolveRoleEntry(role: AuthSession['role']) { return { path: entry.path } } -router.beforeEach(async (to) => { +router.beforeEach(async (to, from) => { const authStore = useAuthStore() // 登录页不依赖会话恢复;只使用当前已知身份决定是否跳转。 @@ -41,8 +41,8 @@ router.beforeEach(async (to) => { await authStore.checkAuth() } catch { - // 短暂故障已由请求层重试;阻止导航并保留已有页面,等待恢复连接 - return false + // 首次进入时没有页面可保留,转到登录页;已有页面则保留,且不清除会话。 + return from === START_LOCATION ? login : false } }