import type { Router } from 'vue-router';
|
import { useAuthStore } from '../stores/auth';
|
import NProgress from 'nprogress';
|
import 'nprogress/nprogress.css';
|
|
NProgress.configure({ showSpinner: false });
|
|
export function setupRouterGuard(router: Router) {
|
router.beforeEach((to, from, next) => {
|
NProgress.start();
|
|
const authStore = useAuthStore();
|
const requiresAuth = to.meta.requiresAuth !== false;
|
|
if (requiresAuth && !authStore.isLoggedIn) {
|
// 需要认证但未登录,跳转到登录页
|
next({ path: '/login', query: { redirect: to.fullPath } });
|
} else if (to.path === '/login' && authStore.isLoggedIn) {
|
// 已登录访问登录页,跳转到首页
|
next({ path: '/' });
|
} else {
|
next();
|
}
|
});
|
|
router.afterEach(() => {
|
NProgress.done();
|
});
|
}
|