import { createRouter, createWebHistory } from 'vue-router' import { useAuthStore } from '@/stores/auth' const router = createRouter({ history: createWebHistory(import.meta.env.BASE_URL), routes: [ { path: '/', name: 'home', component: () => import('@/views/HomeView.vue'), meta: { guestOnly: true, }, }, { path: '/login', name: 'login', component: () => import('@/views/LoginView.vue'), meta: { guestOnly: true, }, }, { path: '/signup', name: 'signup', component: () => import('@/views/SignupView.vue'), meta: { guestOnly: true, }, }, { path: '/check-email', name: 'check-email', component: () => import('@/views/CheckEmailView.vue'), meta: { guestOnly: true, }, beforeEnter: () => { if (!useAuthStore().signupCompleted) { return { name: 'signup' } } }, }, { path: '/confirm-email', name: 'confirm-email', component: () => import('@/views/ConfirmEmailView.vue'), meta: { guestOnly: true, }, beforeEnter: (to) => { if (typeof to.query.token !== 'string' || to.query.token === '') { return { name: 'signup' } } }, }, { path: '/dashboard', name: 'dashboard', component: () => import('@/views/DashboardView.vue'), meta: { requiresAuth: true, }, }, ], }) router.beforeEach(async (to) => { const authStore = useAuthStore() if (to.meta.requiresAuth && !authStore.isAuthenticated) { const restoredSession = await authStore.fetchMe() if (!restoredSession) { return { name: 'login', query: { redirect: to.fullPath, }, } } } if (to.meta.guestOnly) { let restoredSession = authStore.isAuthenticated if (!restoredSession) { restoredSession = await authStore.fetchMe() } if (restoredSession) { return { name: 'dashboard', } } } }) export default router