import { createRouter, createWebHistory } from 'vue-router' import { useAuthStore } from '@/stores/auth' export const router = createRouter({ history: createWebHistory(), routes: [ { path: '/', name: 'landing', component: () => import('@/views/LandingView.vue') }, { path: '/login', name: 'login', component: () => import('@/views/LoginView.vue'), meta: { guest: true } }, { path: '/register', name: 'register', component: () => import('@/views/RegisterView.vue'), meta: { guest: true } }, { path: '/console', component: () => import('@/views/console/ConsoleLayout.vue'), meta: { auth: true }, children: [ { path: '', redirect: '/console/dashboard' }, { path: 'dashboard', name: 'dashboard', component: () => import('@/views/console/DashboardView.vue') }, { path: 'keys', name: 'keys', component: () => import('@/views/console/KeysView.vue') }, { path: 'usage', name: 'usage', component: () => import('@/views/console/UsageView.vue') }, ], }, { path: '/admin', component: () => import('@/views/admin/AdminLayout.vue'), meta: { auth: true, admin: true }, children: [ { path: '', redirect: '/admin/overview' }, { path: 'overview', name: 'admin-overview', component: () => import('@/views/admin/OverviewView.vue') }, { path: 'channels', name: 'admin-channels', component: () => import('@/views/admin/ChannelsView.vue') }, { path: 'models', name: 'admin-models', component: () => import('@/views/admin/ModelsView.vue') }, { path: 'users', name: 'admin-users', component: () => import('@/views/admin/UsersView.vue') }, { path: 'config', name: 'admin-config', component: () => import('@/views/admin/ConfigView.vue') }, ], }, { path: '/:pathMatch(.*)*', redirect: '/' }, ], }) router.beforeEach(async (to) => { const auth = useAuthStore() if (!auth.ready) await auth.bootstrap() if (to.meta.auth && !auth.isAuthed) return { name: 'login', query: { redirect: to.fullPath } } if (to.meta.admin && !auth.isAdmin) return { name: 'dashboard' } if (to.meta.guest && auth.isAuthed) return { name: 'dashboard' } return true })