| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106 |
- 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
|