import { createRouter, createWebHistory } from 'vue-router'; import { useAuthStore } from '@/stores/auth'; import { useProfileModalStore } from '@/stores/profile-modal'; import { useOwnedBusiness } from '@/composables/useOwnedBusiness'; import { loginRouteWithRedirect } from '@/utils/auth-redirect'; const router = createRouter({ history: createWebHistory(), routes: [ { path: '/', redirect: '/app' }, { path: '/login', name: 'login', component: () => import('@/views/auth/LoginView.vue'), meta: { guest: true }, }, { path: '/verify', name: 'verify', component: () => import('@/views/auth/VerifyOtpView.vue'), meta: { guest: true }, }, { path: '/app', name: 'app-home', component: () => import('@/views/app/AppHomeView.vue'), }, { path: '/app/businesses/:id', name: 'app-business-detail', component: () => import('@/views/app/AppBusinessDetailView.vue'), }, { path: '/app/bookings', name: 'app-bookings', component: () => import('@/views/app/AppBookingsView.vue'), meta: { requiresAuth: true }, }, { path: '/app/profile', name: 'app-profile', meta: { requiresAuth: true }, redirect: () => { useProfileModalStore().open(); return { name: 'app-home' }; }, }, { path: '/app/register-business', name: 'app-register-business', component: () => import('@/views/app/AppRegisterBusinessView.vue'), meta: { requiresAuth: true, requiresCustomer: true }, }, { path: '/app/owner', name: 'app-owner', component: () => import('@/views/app/AppOwnerView.vue'), meta: { requiresAuth: true, requiresBusinessOwner: true }, }, { path: '/home', redirect: '/app' }, ], }); router.beforeEach(async (to) => { const auth = useAuthStore(); if (auth.token && !auth.user) { await auth.loadUser(); } if (to.meta.requiresAuth && !auth.isLoggedIn) { return loginRouteWithRedirect(to.fullPath); } if (to.meta.requiresBusinessOwner && !auth.isBusinessOwner && !auth.isAdmin) { return { name: 'app-home' }; } if (to.meta.requiresCustomer && !auth.isCustomer) { return auth.isBusinessOwner ? { name: 'app-owner' } : { name: 'app-home' }; } if (to.name === 'app-business-detail' && auth.isBusinessOwner) { const { ensureLoaded, isOwnBusiness } = useOwnedBusiness(); await ensureLoaded(); if (isOwnBusiness(String(to.params.id))) { return { name: 'app-owner' }; } } if (to.meta.guest && auth.isLoggedIn) { return { name: 'app-home' }; } }); export default router;