index.js 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. import { createRouter, createWebHistory } from 'vue-router'
  2. import { useAuthStore } from '@/stores/auth'
  3. const routes = [
  4. {
  5. path: '/login',
  6. name: 'Login',
  7. component: () => import('@/views/auth/LoginView.vue'),
  8. meta: { requiresAuth: false }
  9. },
  10. {
  11. path: '/',
  12. redirect: '/dashboard'
  13. },
  14. {
  15. path: '/dashboard',
  16. name: 'Dashboard',
  17. component: () => import('@/views/DashboardView.vue'),
  18. meta: { requiresAuth: true }
  19. },
  20. {
  21. path: '/superadmin',
  22. component: () => import('@/layouts/SuperadminLayout.vue'),
  23. meta: { requiresAuth: true, requiresSuperadmin: true },
  24. children: [
  25. {
  26. path: '',
  27. name: 'SuperadminDashboard',
  28. component: () => import('@/views/superadmin/DashboardView.vue')
  29. },
  30. {
  31. path: 'organizations',
  32. name: 'Organizations',
  33. component: () => import('@/views/superadmin/OrganizationsView.vue')
  34. },
  35. {
  36. path: 'devices',
  37. name: 'SuperadminDevices',
  38. component: () => import('@/views/superadmin/DevicesView.vue')
  39. },
  40. {
  41. path: 'users',
  42. name: 'SuperadminUsers',
  43. component: () => import('@/views/superadmin/UsersView.vue')
  44. }
  45. ]
  46. },
  47. {
  48. path: '/client',
  49. component: () => import('@/layouts/ClientLayout.vue'),
  50. meta: { requiresAuth: true },
  51. children: [
  52. {
  53. path: '',
  54. name: 'ClientDashboard',
  55. component: () => import('@/views/client/DashboardView.vue')
  56. },
  57. {
  58. path: 'devices',
  59. name: 'ClientDevices',
  60. component: () => import('@/views/client/DevicesView.vue')
  61. },
  62. {
  63. path: 'users',
  64. name: 'ClientUsers',
  65. component: () => import('@/views/client/UsersView.vue')
  66. }
  67. ]
  68. }
  69. ]
  70. const router = createRouter({
  71. history: createWebHistory(),
  72. routes
  73. })
  74. // Navigation guards
  75. router.beforeEach((to, from, next) => {
  76. const authStore = useAuthStore()
  77. // Если маршрут требует аутентификации
  78. if (to.meta.requiresAuth && !authStore.isAuthenticated) {
  79. next('/login')
  80. return
  81. }
  82. // Если маршрут требует superadmin права
  83. if (to.meta.requiresSuperadmin && !authStore.isSuperadmin) {
  84. next('/dashboard')
  85. return
  86. }
  87. // Если аутентифицирован и пытается зайти на /login
  88. if (to.path === '/login' && authStore.isAuthenticated) {
  89. if (authStore.isSuperadmin) {
  90. next('/superadmin')
  91. } else {
  92. next('/client')
  93. }
  94. return
  95. }
  96. next()
  97. })
  98. export default router