pnpm-test/src/core/setup/router.setup.ts
2025-12-17 14:37:40 +05:45

26 lines
809 B
TypeScript

import router from "@/router";
import { useAuth } from "@/stores/Auth/auth.store";
import type { App } from "vue";
const setupRouter = (app: App) => {
router.beforeEach(async (to, from, next) => {
if (to.meta.requireAuth || to.meta.onlyGuest) {
const authStore = useAuth();
await authStore.getUser();
if (authStore.isAuthenticated && to.meta.requireAuth) {
next();
} else if (!authStore.isAuthenticated && to.meta.onlyGuest) {
next();
} else if (authStore.isAuthenticated && to.meta.onlyGuest) {
next({ name: "dashboard" });
} else {
next({ name: "login" });
}
}
next();
});
app.use(router);
};
export default setupRouter;