import { createRouter, createWebHistory } from "vue-router"; import { useAuthStore } from "../stores/authStore"; import { useSettingsStore } from "../stores/settingsStore"; import LoginPage from "../pages/Login.vue"; import RegisterPage from "../pages/Register.vue"; const routes = [ { path: "/login", name: "Login", component: LoginPage, }, { path: "/register", name: "Register", component: RegisterPage, }, { path: "/", name: "Home", component: () => import("../pages/Home.vue"), meta: { requiresAuth: true }, }, { path: "/search", name: "Search", component: () => import("../pages/Home.vue"), meta: { requiresAuth: true }, }, { path: "/admin", name: "Admin", component: () => import("../pages/Admin.vue"), meta: { requiresAuth: true, requiresAdminPermission: true }, }, { path: "/s/:spaceId", name: "PublicSpace", component: () => import("../pages/PublicSpace.vue"), }, { path: "/s/:spaceId/n/:noteId", name: "PublicNote", component: () => import("../pages/PublicSpace.vue"), }, ]; const router = createRouter({ history: createWebHistory(), routes, }); router.beforeEach(async (to, from, next) => { const authStore = useAuthStore(); const settingsStore = useSettingsStore(); await authStore.ensureInitialized(); if (to.path === "/register") { await settingsStore.loadFeatureFlags(); if (!settingsStore.registrationEnabled) { next({ path: "/login", query: { message: "Registration is currently disabled." } }); return; } } if (to.meta.requiresAuth && !authStore.isAuthenticated) { next("/login"); } else if (to.meta.requiresAdminPermission && !authStore.isAdmin) { next("/"); } else if ((to.path === "/login" || to.path === "/register") && authStore.isAuthenticated) { next("/"); } else { next(); } }); export default router;