import { createRouter, createWebHistory } from 'vue-router' import { useAuthStore } from '../stores/auth' const router = createRouter({ history: createWebHistory(import.meta.env.BASE_URL), routes: [ { path: '/', redirect: { name: 'dashboard' } }, { path: '/dashboard', name: 'dashboard', component: () => import('../views/DashboardView.vue'), meta: { requiresAuth: true, wide: true }, }, { path: '/projects/:id(\\d+)', name: 'project', component: () => import('../views/ProjectView.vue'), meta: { requiresAuth: true, wide: true }, }, { path: '/profile', name: 'profile', component: () => import('../views/ProfileView.vue'), meta: { requiresAuth: true }, }, { // Magic-link target. Works signed in or out — verifying returns a session. path: '/verify-email', name: 'verify-email', component: () => import('../views/VerifyEmailView.vue'), }, { path: '/login', name: 'login', component: () => import('../views/LoginView.vue'), meta: { guestOnly: true }, }, { path: '/:pathMatch(.*)*', redirect: { name: 'dashboard' } }, ], }) const DEFAULT_PATHS = new Set(['/', '/dashboard']) router.beforeEach((to) => { const auth = useAuthStore() if (to.meta.requiresAuth && !auth.isAuthenticated) { return { name: 'login', query: DEFAULT_PATHS.has(to.path) ? {} : { redirect: to.fullPath }, } } if (to.meta.guestOnly && auth.isAuthenticated) { return { name: 'dashboard' } } }) export default router