index.js 2.8 KB
Newer Older
abergavenny's avatar
abergavenny committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
import { createRouter, createWebHistory } from 'vue-router'

import LandingView from '@/views/LandingView.vue'
import NotFoundView from '@/views/NotFoundView.vue'
import ResetPasswordView from '@/views/ResetPasswordView.vue'
import TermsView from '@/views/TermsView.vue'
import { useSessionStore } from '@/stores/session'

const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
    {
      path: '/',
      name: 'landing',
      component: LandingView
    },
    {
      path: '/terms',
      name: 'terms',
      component: TermsView
    },
    {
      path: '/reset-password',
      name: 'reset',
      component: ResetPasswordView
    },
    {
      path: '/apartments',
      name: 'apartments',
      redirect: {
        name: 'apartments.dashboard'
      },
      component: () => import('../views/ApartmentView.vue'),
      beforeEnter: (to) => {
        try {
          const store = useSessionStore()

          if (to.meta.requiresAuth && store.isAuthenticated && store.currentUser?.role === 'user') return true

          return { name: 'landing' }
        } catch (error) {
          console.error('ERROR:', error.name)
          return { name: 'landing' }
        }
      },
      children: [
        {
          path: '',
          name: 'apartments.dashboard',
          component: () => import('../components/ApartmentDashboard.vue'),
        },
      ],
      meta: {
        requiresAuth: true
      }
    },
    {
      path: '/buildings',
      name: 'buildings',
      redirect: {
        name: 'buildings.dashboard'
      },

      component: () => import('../views/BuildingView.vue'),
      beforeEnter: (to) => {
        try {
          const store = useSessionStore()

          if (to.meta.requiresAuth && store.isAuthenticated && store.currentUser?.role === 'administrator') return true

          return { name: 'landing' }
        } catch (error) {
          console.error('ERROR:', error.name)

          return { name: 'landing' }
        }
      },
      children: [
        {
          path: '',
          name: 'buildings.dashboard',
          redirect: {
            name: 'buildings.dashboard.home'
          },
          component: () => import('../components/BuildingDashboard.vue'),
          children: [
            {
              path: '',
              name: 'buildings.dashboard.home',
              component: () => import('../components/BuildingApartments.vue'),
            },
            {
              path: 'apartment',
              name: 'buildings.dashboard.apartment',
              component: () => import('../components/BuildingApartmentDetails.vue'),
            },
          ]
        },
      ],
      meta: {
        requiresAuth: true
      }
    },
    { path: '/:pathMatch(.*)*', name: 'not-found', component: NotFoundView }
  ]
})

export default router