fix: fix lost token validation
This commit is contained in:
@@ -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 };
|
||||
|
||||
Reference in New Issue
Block a user