เพิ่ม login keycloak

This commit is contained in:
Kittapath 2023-11-15 09:51:52 +07:00
parent d47ea34dd9
commit aad843fec9
7 changed files with 160 additions and 4 deletions

View file

@ -1,6 +1,8 @@
import { createRouter, createWebHistory } from 'vue-router'
import HomeView from '@/views/HomeView.vue'
import keycloak from '@/plugins/keycloak'
const router = createRouter({
history: createWebHistory(import.meta.env.BASE_URL),
routes: [
@ -8,6 +10,9 @@ const router = createRouter({
path: '/',
name: 'home',
component: HomeView,
meta: {
Auth: true,
},
},
{
path: '/about',
@ -16,11 +21,17 @@ const router = createRouter({
// this generates a separate chunk (about.[hash].js) for this route
// which is lazy-loaded when the route is visited.
component: () => import('@/views/AboutView.vue'),
meta: {
Auth: true,
},
},
{
path: '/history',
name: 'history',
component: () => import('@/views/HistoryView.vue'),
meta: {
Auth: true,
},
},
// {
// path: '/camera',
@ -30,13 +41,39 @@ const router = createRouter({
/**
* 404 Not Found
* ref: https://router.vuejs.org/guide/essentials/dynamic-matching.html#catch-all-404-not-found-route
*/
*/
{
path: '/:pathMatch(.*)*',
name: 'NotFound',
component: () => import('@/views/ErrorNotFoundPage.vue'),
},
meta: {
Auth: true,
},
},
],
})
router.beforeEach((to, from, next) => {
if (to.meta.Auth) {
if (!keycloak.authenticated) {
keycloak.login({
redirectUri: `${window.location.protocol}//${window.location.host}${to.path}`,
locale: 'th',
})
} else {
// keycloak.updateToken(60);
// const role = keycloak.tokenParsed?.role
// if (role.includes(to.meta.Role)) {
next()
// } else {
// next({ path: '' })
// // next();
// }
}
} else {
next()
}
// next();
})
export default router