From 1a9eb9299b418c77c720ef7ac400ab4f4db565cd Mon Sep 17 00:00:00 2001 From: yyc12345 Date: Tue, 21 Jul 2026 14:54:46 +0800 Subject: [PATCH] fix: fix lost token validation --- frontend/src/router/index.ts | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/frontend/src/router/index.ts b/frontend/src/router/index.ts index b1a6b48..b482dfd 100644 --- a/frontend/src/router/index.ts +++ b/frontend/src/router/index.ts @@ -1,5 +1,6 @@ import { createRouter, createWebHistory } from 'vue-router' import { useTokenStore } from '@/stores/token' +import { tokenValid as apiCommonTokenValid } from '@/api/common' import Home from '@/views/Home.vue' import Collection from '@/views/Collection.vue' @@ -33,9 +34,19 @@ const router = createRouter({ routes: routes, }); -router.beforeEach((to, _from) => { - // Only check for those flagged. +router.beforeEach(async (to, _from) => { const token = useTokenStore(); + + // Sync the local token with the server on every navigation. If the server + // says it's invalid (or the request fails — faithfully preserved from the + // legacy behaviour, where any failure cleared the token), log out so the + // meta checks below and the reactive nav see the logged-out state. + if (token.isLoggedIn) { + const ok = await apiCommonTokenValid(token.currentToken); + if (!ok) token.logout(); + } + + // Only check for those flagged. if (to.meta.requireLoggedInCheck) { if (!token.isLoggedIn) { return { name: 'Default', replace: true };