feat: 更新超级管理员登录逻辑,添加路由守卫和用户统计功能
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
import AppLayout from '@/layout/AppLayout.vue';
|
||||
import { createRouter, createWebHistory } from 'vue-router';
|
||||
import { clearSuperAuthToken, hasSuperAuthToken, refreshSuperToken } from '@/service/auth';
|
||||
|
||||
const router = createRouter({
|
||||
history: createWebHistory('/super/'),
|
||||
@@ -7,6 +8,7 @@ const router = createRouter({
|
||||
{
|
||||
path: '/',
|
||||
component: AppLayout,
|
||||
meta: { requiresAuth: true },
|
||||
children: [
|
||||
{
|
||||
path: '/',
|
||||
@@ -153,4 +155,34 @@ const router = createRouter({
|
||||
]
|
||||
});
|
||||
|
||||
let tokenValidated = false;
|
||||
let tokenValidationPromise = null;
|
||||
|
||||
router.beforeEach(async (to) => {
|
||||
if (to.meta?.requiresAuth !== true) return true;
|
||||
|
||||
const isAuthed = hasSuperAuthToken();
|
||||
if (!isAuthed) {
|
||||
return {
|
||||
name: 'login',
|
||||
query: { redirect: to.fullPath }
|
||||
};
|
||||
}
|
||||
|
||||
if (!tokenValidated) {
|
||||
tokenValidationPromise ??= refreshSuperToken();
|
||||
try {
|
||||
await tokenValidationPromise;
|
||||
tokenValidated = true;
|
||||
} catch {
|
||||
clearSuperAuthToken();
|
||||
tokenValidated = false;
|
||||
tokenValidationPromise = null;
|
||||
return { name: 'login', query: { redirect: to.fullPath } };
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
});
|
||||
|
||||
export default router;
|
||||
|
||||
Reference in New Issue
Block a user