refactor: add roles admin and user

This commit is contained in:
somnetsak123 2023-11-28 09:22:44 +07:00 committed by Methapon2001
parent 72888c407e
commit a28bc7de6b
No known key found for this signature in database
GPG key ID: 849924FEF46BD132
3 changed files with 59 additions and 29 deletions

View file

@ -10,6 +10,26 @@ const router = createRouter({
path: '/',
name: 'UserModule',
component: () => import('@/views/MainLayout.vue'),
beforeEnter: (_to, _from, next) => {
const token = KeyCloakService.GetAccesToken()
if (token) {
next()
} else {
KeyCloakService.CallLogin(() => {
const tokenAfterLogin = KeyCloakService.GetAccesToken()
const roles = KeyCloakService.GetUserRoles()
console.log(roles);
if (tokenAfterLogin && (roles.includes('user') || roles.includes('admin')) ) {
next()
} else {
console.error('ไม่สามารถดึง Token หลังจากล็อกอินได้')
next('/')
}
})
}
},
children: [...UserModule],
meta: {
statusAccount: false,
@ -26,7 +46,11 @@ const router = createRouter({
} else {
KeyCloakService.CallLogin(() => {
const tokenAfterLogin = KeyCloakService.GetAccesToken()
if (tokenAfterLogin) {
const roles = KeyCloakService.GetUserRoles()
console.log(roles);
if (tokenAfterLogin && roles.includes('admin') ) {
next()
} else {
console.error('ไม่สามารถดึง Token หลังจากล็อกอินได้')