* authentication for specific routes

feature: #1
This commit is contained in:
2023-01-01 02:07:21 +01:00
parent af3b12344f
commit 23c40c5468
10 changed files with 145 additions and 11 deletions

View File

@@ -1,26 +1,58 @@
import { createRouter, createWebHistory } from 'vue-router'
import HomeView from '../views/HomeView.vue'
import AuthHelper from '../authentication/AuthHelper'
const router = createRouter({
history: createWebHistory(import.meta.env.BASE_URL),
routes: [
{
path: '/',
name: 'home',
component: HomeView
name: 'main',
component: HomeView,
meta: {
requiresAuth: false
}
},
{
path: '/about',
name: 'about',
// this page is lazy-loaded when the route is visited.
component: () => import('../views/AboutView.vue')
component: () => import('../views/AboutView.vue'),
meta: {
requiresAuth: true
}
},
{
path: '/forbidden',
name: 'forbidden',
component: () => import('../components/ForbiddenPage.vue'),
meta: {
requiresAuth: false
}
},
{
path: '/:pathMatch(.*)*',
name: 'notfound',
component: () => import('../components/PageNotFound.vue')
component: () => import('../components/PageNotFound.vue'),
meta: {
requiresAuth: false
}
}
]
})
router.beforeEach(async (to, from, next) => {
if (to.meta.requiresAuth === true) {
if (AuthHelper.getKeycloak() === null) {
await AuthHelper.setup()
}
if (!AuthHelper.getKeycloak().authenticated) {
next('/forbidden')
}
next()
} else {
next()
}
})
export default router