import { createRouter, createWebHistory } from 'vue-router' import { getCachedUser, isAuthenticated } from '@/services/authService' const router = createRouter({ history: createWebHistory(import.meta.env.BASE_URL), routes: [ { path: '/welcome', name: 'welcome', component: () => import('@/views/auth/WelcomeView.vue'), meta: { guest: true, hideNav: true }, }, { path: '/login', name: 'login', component: () => import('@/views/auth/LoginView.vue'), meta: { guest: true, hideNav: true }, }, { path: '/register', name: 'register', component: () => import('@/views/auth/RegisterView.vue'), meta: { guest: true, hideNav: true }, }, { path: '/otp', name: 'otp', component: () => import('@/views/auth/OtpView.vue'), meta: { guest: true, hideNav: true }, }, { path: '/', name: 'home', component: () => import('@/views/home/HomeView.vue'), meta: { requiresAuth: true }, }, { path: '/companies', redirect: '/categories', }, { path: '/categories', name: 'categories', component: () => import('@/views/categories/CategoriesView.vue'), meta: { requiresAuth: true }, }, { path: '/products', name: 'products', component: () => import('@/views/products/ProductsView.vue'), meta: { requiresAuth: true }, }, { path: '/product', name: 'product', component: () => import('@/views/products/ProductDetailView.vue'), meta: { requiresAuth: true }, }, { path: '/catalogs', name: 'catalogs', component: () => import('@/views/catalogs/CatalogsHubView.vue'), meta: { requiresAuth: true }, }, { path: '/catalogs/:catalogId', name: 'catalog-detail', component: () => import('@/views/catalogs/CatalogProductRedirectView.vue'), meta: { requiresAuth: true }, }, { path: '/catalog-requests', name: 'catalog-requests', component: () => import('@/views/catalogs/CatalogRequestsView.vue'), meta: { requiresAuth: true }, }, { path: '/catalog-requests/latest-status', name: 'catalog-request-latest-status', component: () => import('@/views/catalogs/LatestCatalogRequestStatusView.vue'), meta: { requiresAuth: true }, }, { path: '/favorites', name: 'favorites', component: () => import('@/views/products/FavoritesView.vue'), meta: { requiresAuth: true }, }, { path: '/customers', name: 'customers', component: () => import('@/views/customers/CustomersView.vue'), meta: { requiresAuth: true, agentOnly: true }, }, { path: '/customers/new', redirect: (to) => ({ path: '/customers', query: { ...to.query, create: '1' }, }), }, { path: '/customer', name: 'customer', component: () => import('@/views/customers/CustomerDetailView.vue'), meta: { requiresAuth: true, agentOnly: true }, }, { path: '/meeting-report', redirect: (to) => ({ path: '/customer', query: { id: String(to.query.customerId ?? ''), tab: 'reports', meetingReport: '1', }, }), }, { path: '/reminders', name: 'reminders', component: () => import('@/views/reminders/RemindersView.vue'), meta: { requiresAuth: true, agentOnly: true }, }, { path: '/reminder', name: 'reminder', component: () => import('@/views/reminders/ReminderDetailView.vue'), meta: { requiresAuth: true, agentOnly: true, hideNav: true }, }, { path: '/companies/:companyId/catalogs', name: 'company-catalogs', component: () => import('@/views/catalogs/CatalogListView.vue'), meta: { requiresAuth: true }, }, { path: '/companies/:companyId/catalogs/:catalogId/products', name: 'company-products', redirect: (to) => ({ path: '/products', query: { catalogId: String(to.params.catalogId ?? ''), companyId: String(to.params.companyId ?? ''), }, }), }, { path: '/cart', name: 'cart', component: () => import('@/views/cart/CartView.vue'), meta: { requiresAuth: true }, }, { path: '/orders', name: 'orders', component: () => import('@/views/OrdersView.vue'), meta: { requiresAuth: true }, }, { path: '/orders/latest-status', name: 'order-latest-status', component: () => import('@/views/orders/LatestOrderStatusView.vue'), meta: { requiresAuth: true }, }, { path: '/order', name: 'order-query', redirect: (to) => { const id = String(to.query.id ?? '') if (!id) return { name: 'orders' } return { path: `/orders/${id}` } }, }, { path: '/orders/:id', name: 'order-detail', component: () => import('@/views/orders/OrderDetailView.vue'), meta: { requiresAuth: true }, }, { path: '/orders/:id/success', name: 'order-success', component: () => import('@/views/orders/OrderSuccessView.vue'), meta: { requiresAuth: true, hideNav: true }, }, { path: '/orders/:id/settlement', name: 'settlement', component: () => import('@/views/checkout/SettlementTypeView.vue'), meta: { requiresAuth: true, hideNav: true }, }, { path: '/orders/:id/cash', name: 'cash-settlement', component: () => import('@/views/checkout/CashSettlementView.vue'), meta: { requiresAuth: true, hideNav: true }, }, { path: '/orders/:id/check-mode', name: 'check-mode', component: () => import('@/views/checkout/CheckModeSelectView.vue'), meta: { requiresAuth: true, hideNav: true }, }, { path: '/orders/:id/check-form', name: 'check-form', component: () => import('@/views/checkout/CheckFormView.vue'), meta: { requiresAuth: true, hideNav: true }, }, { path: '/orders/:id/check-list', name: 'check-list', component: () => import('@/views/checkout/CheckListView.vue'), meta: { requiresAuth: true, hideNav: true }, }, { path: '/profile', name: 'profile', component: () => import('@/views/profile/ProfileView.vue'), meta: { requiresAuth: true }, }, { path: '/profile/edit', name: 'profile-edit', component: () => import('@/views/profile/ProfileEditView.vue'), meta: { requiresAuth: true }, }, { path: '/profile/security', name: 'profile-security', component: () => import('@/views/profile/ProfileSecurityView.vue'), meta: { requiresAuth: true }, }, { path: '/profile/notifications', name: 'profile-notifications', component: () => import('@/views/profile/ProfileNotificationsView.vue'), meta: { requiresAuth: true }, }, { path: '/profile/display', name: 'profile-display', component: () => import('@/views/profile/ProfileDisplayView.vue'), meta: { requiresAuth: true }, }, { path: '/profile/checks', name: 'profile-checks', component: () => import('@/views/profile/ProfileChecksView.vue'), meta: { requiresAuth: true }, }, ], scrollBehavior() { return { top: 0 } }, }) router.beforeEach((to) => { const authed = isAuthenticated() if (to.meta.requiresAuth && !authed) { if (to.name === 'home' || to.path === '/') { return { name: 'welcome' } } return { name: 'welcome', query: { redirect: to.fullPath } } } if (to.meta.guest && authed) { return { name: 'home' } } if (to.meta.agentOnly) { const user = getCachedUser() if (user?.appRole !== 'agent') { return { name: 'home' } } } return true }) export default router