feat: 添加用户认证功能,优化登录和注册流程,集成表单验证和加载状态

This commit is contained in:
2026-01-17 21:18:51 +07:00
parent 7ec2522fa0
commit 51719cd229
12 changed files with 252 additions and 73 deletions

View File

@@ -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();
});
}