import { createRouter, createWebHistory } from 'vue-router' import { useAuthStore } from '@/stores/auth' const routes = [ { path: '/login', name: 'Login', component: () => import('@/views/auth/LoginView.vue'), meta: { requiresAuth: false } }, { path: '/', redirect: '/dashboard' }, { path: '/dashboard', name: 'Dashboard', component: () => import('@/views/DashboardView.vue'), meta: { requiresAuth: true } }, { path: '/superadmin', component: () => import('@/layouts/SuperadminLayout.vue'), meta: { requiresAuth: true, requiresSuperadmin: true }, children: [ { path: '', name: 'SuperadminDashboard', component: () => import('@/views/superadmin/DashboardView.vue') }, { path: 'organizations', name: 'Organizations', component: () => import('@/views/superadmin/OrganizationsView.vue') }, { path: 'devices', name: 'SuperadminDevices', component: () => import('@/views/superadmin/DevicesView.vue') }, { path: 'users', name: 'SuperadminUsers', component: () => import('@/views/superadmin/UsersView.vue') } ] }, { path: '/client', component: () => import('@/layouts/ClientLayout.vue'), meta: { requiresAuth: true }, children: [ { path: '', name: 'ClientDashboard', component: () => import('@/views/client/DashboardView.vue') }, { path: 'devices', name: 'ClientDevices', component: () => import('@/views/client/DevicesView.vue') }, { path: 'users', name: 'ClientUsers', component: () => import('@/views/client/UsersView.vue') } ] } ] const router = createRouter({ history: createWebHistory(), routes }) // Navigation guards router.beforeEach((to, from, next) => { const authStore = useAuthStore() // Если маршрут требует аутентификации if (to.meta.requiresAuth && !authStore.isAuthenticated) { next('/login') return } // Если маршрут требует superadmin права if (to.meta.requiresSuperadmin && !authStore.isSuperadmin) { next('/dashboard') return } // Если аутентифицирован и пытается зайти на /login if (to.path === '/login' && authStore.isAuthenticated) { if (authStore.isSuperadmin) { next('/superadmin') } else { next('/client') } return } next() }) export default router