import { createRouter, createWebHistory } from 'vue-router' import { canAccessAdminRoute, clearAdminBootstrapSession, ensureAdminAuthBootstrap, getAdminBootstrapSnapshot, resolveAdminLandingPath, } from '@/utils/admin-auth-bootstrap' const pageNames = [ ['', 'HomeView', 'admin', undefined], ['users', 'UsersView', 'admin-users', 'user:view'], ['registrations', 'WorkbenchRegistrationView', 'admin-registrations', 'user:view'], ['privacy', 'PrivacyComplianceView', 'admin-privacy', 'legal_agreement:manage'], ['shops', 'ShopsView', 'admin-shops', 'shop:view'], ['profile', 'ProfileView', 'admin-profile', undefined], ['settings', 'SystemSettingsView', 'admin-settings', 'messagehub:config:manage'] ] as const const routes = [ { path: '/', redirect: '/admin' }, { path: '/admin/login', name: 'admin-login', component: () => import('../views/admin/AdminLoginView.vue'), meta: { requiresAuth: false, isAdmin: true, hideNav: true } }, { path: '/admin', component: () => import('../views/admin/AdminLayout.vue'), meta: { requiresAuth: true, isAdmin: true, hideNav: true }, children: pageNames.map(([path, view, name, permissionCode]) => ({ path, name, component: () => import(`../views/admin/${view}.vue`), meta: { requiresAuth: true, isAdmin: true, hideNav: true, permissionCode } })) }, { path: '/:pathMatch(.*)*', name: 'not-found', component: () => import('../views/NotFoundView.vue'), meta: { requiresAuth: false, isAdmin: false, hideNav: true } } ] const router = createRouter({ history: createWebHistory(import.meta.env.BASE_URL), routes }) router.beforeEach(async to => { if (!to.meta.isAdmin) return true const snapshot = getAdminBootstrapSnapshot() if (to.path === '/admin/login') { if (!snapshot.token || !snapshot.userInfo) return true return (await ensureAdminAuthBootstrap()) === 'ready' ? resolveAdminLandingPath() : true } if (!snapshot.isPlatformAdmin) { clearAdminBootstrapSession() return '/admin/login' } if (!snapshot.token || !snapshot.userInfo) { clearAdminBootstrapSession() return '/admin/login' } if (await ensureAdminAuthBootstrap() !== 'ready') return '/admin/login' return canAccessAdminRoute(to.path, to.meta.permissionCode as string | undefined) ? true : resolveAdminLandingPath() }) export default router