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