fix: fix lost token validation

This commit is contained in:
2026-07-21 14:54:46 +08:00
parent 36ce59c4ab
commit 1a9eb9299b
+13 -2
View File
@@ -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 };