feat: 添加用户认证功能,优化登录和注册流程,集成表单验证和加载状态
This commit is contained in:
@@ -1,15 +1,24 @@
|
||||
import type { Router } from "vue-router";
|
||||
import type { RouteLocation, Router } from "vue-router";
|
||||
import authRoutes from "./auth";
|
||||
|
||||
function isAuthRoute(route: RouteLocation) {
|
||||
return authRoutes.some(r => r.path === route.path);
|
||||
}
|
||||
|
||||
export function createRouterGuard(router: Router) {
|
||||
router.beforeEach(async (to, from, next) => {
|
||||
const userStore = useUserStore();
|
||||
// if (to.meta.requiresAuth && !userStore.isAuthenticated) {
|
||||
// if (from.path === "/auth/login") {
|
||||
// return next("/");
|
||||
// }
|
||||
// const redirect = encodeURIComponent(to.fullPath);
|
||||
// return next({ path: "/auth/login", query: { redirect } });
|
||||
// }
|
||||
if (to.meta.requiresAuth && !userStore.isAuthenticated) {
|
||||
if (isAuthRoute(from)) {
|
||||
return next("/");
|
||||
}
|
||||
const redirect = encodeURIComponent(to.fullPath);
|
||||
return next({ path: "/auth/login", query: { redirect } });
|
||||
}
|
||||
|
||||
if (isAuthRoute(to) && userStore.isAuthenticated) {
|
||||
return next("/");
|
||||
}
|
||||
next();
|
||||
});
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user