import type { Pinia } from 'pinia' import { createRouter, createWebHistory, type RouteRecordRaw } from 'vue-router' import { useAuthStore } from '@/stores/auth' export function createAppRouter(pinia: Pinia) { const routes: RouteRecordRaw[] = [ { path: '/', component: () => import('@/app/layouts/PublicLayout.vue'), children: [ { path: '', name: 'home', component: () => import('@/pages/HomePage.vue') }, { path: 'pricing', name: 'pricing', component: () => import('@/pages/PricingPage.vue') }, { path: 'docs', name: 'docs', component: () => import('@/pages/DocsPage.vue') }, { path: 'login', name: 'login', component: () => import('@/pages/LoginPage.vue') }, { path: 'register', name: 'register', component: () => import('@/pages/RegisterPage.vue') }, { path: 'verify-email', name: 'verify-email', component: () => import('@/pages/VerifyEmailPage.vue') }, { path: 'forgot-password', name: 'forgot-password', component: () => import('@/pages/ForgotPasswordPage.vue') }, { path: 'reset-password', name: 'reset-password', component: () => import('@/pages/ResetPasswordPage.vue') }, { path: 'terms', name: 'terms', component: () => import('@/pages/TermsPage.vue') }, { path: 'privacy', name: 'privacy', component: () => import('@/pages/PrivacyPage.vue') }, ], }, { path: '/dashboard', component: () => import('@/app/layouts/DashboardLayout.vue'), meta: { requiresAuth: true }, children: [ { path: '', name: 'dashboard', component: () => import('@/pages/dashboard/DashboardHomePage.vue') }, { path: 'history', name: 'dashboard-history', component: () => import('@/pages/dashboard/DashboardHistoryPage.vue'), }, { path: 'api-keys', name: 'dashboard-api-keys', component: () => import('@/pages/dashboard/DashboardApiKeysPage.vue'), }, { path: 'billing', name: 'dashboard-billing', component: () => import('@/pages/dashboard/DashboardBillingPage.vue'), }, { path: 'settings', name: 'dashboard-settings', component: () => import('@/pages/dashboard/DashboardSettingsPage.vue'), }, ], }, { path: '/admin', component: () => import('@/app/layouts/AdminLayout.vue'), meta: { requiresAuth: true, requiresAdmin: true }, children: [ { path: '', name: 'admin', component: () => import('@/pages/admin/AdminHomePage.vue') }, { path: 'users', name: 'admin-users', component: () => import('@/pages/admin/AdminUsersPage.vue') }, { path: 'tasks', name: 'admin-tasks', component: () => import('@/pages/admin/AdminTasksPage.vue') }, { path: 'billing', name: 'admin-billing', component: () => import('@/pages/admin/AdminBillingPage.vue') }, { path: 'redemptions', name: 'admin-redemptions', component: () => import('@/pages/admin/AdminRedemptionPage.vue') }, { path: 'integrations', name: 'admin-integrations', component: () => import('@/pages/admin/AdminIntegrationsPage.vue') }, { path: 'storage', name: 'admin-storage', component: () => import('@/pages/admin/AdminStoragePage.vue') }, { path: 'config', name: 'admin-config', component: () => import('@/pages/admin/AdminConfigPage.vue') }, ], }, { path: '/:pathMatch(.*)*', name: 'not-found', component: () => import('@/pages/NotFoundPage.vue') }, ] const router = createRouter({ history: createWebHistory(), routes, scrollBehavior: () => ({ left: 0, top: 0 }), }) router.beforeEach((to) => { const auth = useAuthStore(pinia) if (to.meta?.requiresAuth && !auth.isLoggedIn) { return { name: 'login', query: { redirect: to.fullPath } } } if ((to.name === 'login' || to.name === 'register') && auth.isLoggedIn) { return { name: auth.user?.role === 'admin' ? 'admin' : 'dashboard' } } if (to.meta?.requiresAdmin && auth.user?.role !== 'admin') { return { name: 'dashboard' } } return true }) return router }