initial commit
This commit is contained in:
68
web/vue-app/src/router/index.ts
Normal file
68
web/vue-app/src/router/index.ts
Normal file
@@ -0,0 +1,68 @@
|
||||
import { createRouter, createWebHistory } from 'vue-router'
|
||||
import { isParentAuthenticated } from '../stores/auth'
|
||||
import ChildLayout from '../layout/ChildLayout.vue'
|
||||
import ChildrenList from '../components/ChildrenList.vue'
|
||||
import ChildView from '../components/ChildView.vue'
|
||||
import ParentView from '../components/ParentView.vue'
|
||||
import ParentLayout from '../layout/ParentLayout.vue'
|
||||
|
||||
const routes = [
|
||||
{
|
||||
path: '/child',
|
||||
name: 'ChildLayout',
|
||||
component: ChildLayout,
|
||||
children: [
|
||||
{
|
||||
path: '', // /child
|
||||
name: 'ChildrenList',
|
||||
component: ChildrenList,
|
||||
},
|
||||
{
|
||||
path: ':id', // /child/:id
|
||||
name: 'ChildDetailView',
|
||||
component: ChildView,
|
||||
props: true,
|
||||
},
|
||||
],
|
||||
},
|
||||
{
|
||||
path: '/parent',
|
||||
name: 'ParentLayout',
|
||||
component: ParentLayout,
|
||||
meta: { requiresAuth: true },
|
||||
children: [
|
||||
{
|
||||
path: '', // /parent
|
||||
name: 'ParentChildrenList',
|
||||
component: ChildrenList,
|
||||
},
|
||||
{
|
||||
path: ':id',
|
||||
name: 'ParentChildDetailView',
|
||||
component: ParentView,
|
||||
props: true,
|
||||
},
|
||||
],
|
||||
},
|
||||
{
|
||||
path: '/',
|
||||
redirect: '/child',
|
||||
},
|
||||
]
|
||||
|
||||
const router = createRouter({
|
||||
history: createWebHistory(import.meta.env.BASE_URL),
|
||||
routes,
|
||||
})
|
||||
|
||||
// Auth guard
|
||||
router.beforeEach((to, from, next) => {
|
||||
if (to.meta.requiresAuth && !isParentAuthenticated.value) {
|
||||
// Redirect to /child if trying to access /parent without auth
|
||||
next('/child')
|
||||
} else {
|
||||
next()
|
||||
}
|
||||
})
|
||||
|
||||
export default router
|
||||
Reference in New Issue
Block a user