fixing auth sso

This commit is contained in:
Warunee Tamkoo 2024-07-16 22:29:09 +07:00
parent ede985939a
commit 93cf472aae
3 changed files with 50 additions and 20 deletions

View file

@ -1,7 +1,7 @@
import { createRouter, createWebHistory } from 'vue-router'
import HomeView from '@/views/HomeView.vue'
import MapView from '@/views/MapView.vue'
import MianView from '@/views/MianView.vue'
import MainView from '@/views/MainView.vue'
import keycloak from '@/plugins/keycloak'
@ -11,7 +11,7 @@ const router = createRouter({
{
path: '/',
name: 'home',
component: MianView,
component: MainView,
children: [
{
path: '/',
@ -45,14 +45,14 @@ const router = createRouter({
name: 'history',
component: () => import('@/views/HistoryView.vue'),
meta: {
Auth: true,
Auth: false,
},
},
// {
// path: '/camera',
// name: 'camera',
// component: () => import('@/views/SampleCamera.vue'),
// },
{
path: '/auth',
name: 'auth',
component: () => import('@/views/auth.vue'),
},
/**
* 404 Not Found
* ref: https://router.vuejs.org/guide/essentials/dynamic-matching.html#catch-all-404-not-found-route
@ -75,18 +75,18 @@ router.beforeEach((to, from, next) => {
console.log('keycloak===>', keycloak)
console.log('keycloak authenticated===>', keycloak.authenticated)
// if (keycloak.authenticated === undefined) {
// window.location.href = 'https://bma-sso.frappet.synology.me'
// } else {
// // keycloak.updateToken(60);
// // const role = keycloak.tokenParsed?.role
// // if (role.includes(to.meta.Role)) {
// next()
// // } else {
// // next({ path: '' })
// // // next();
// // }
// }
if (keycloak.authenticated === undefined) {
window.location.href = 'https://bma-sso.frappet.synology.me'
// } else {
// // keycloak.updateToken(60);
// // const role = keycloak.tokenParsed?.role
// // if (role.includes(to.meta.Role)) {
// next()
// // } else {
// // next({ path: '' })
// // // next();
// // }
}
} else {
next()
}

30
src/views/auth.vue Normal file
View file

@ -0,0 +1,30 @@
<script setup lang="ts">
import { onMounted, ref } from 'vue'
import { useRoute } from 'vue-router'
const route = useRoute()
function setCookie(name: string, value: any, days: number) {
let expires = ''
if (days) {
const date = new Date()
date.setTime(date.getTime() + days * 24 * 60 * 60 * 1000)
expires = '; expires=' + date.toUTCString()
}
document.cookie = name + '=' + (value || '') + expires + '; path=/'
}
onMounted(async () => {
console.log('query', route.query.token)
console.log('accessToken', route.query.accessToken)
setCookie('BMAHRIS_KEYCLOAK_IDENTITY', route.query.token, 1)
setCookie('BMAHRIS_KEYCLOAK_REFRESH', route.query.accessToken, 1)
window.location.href = '/'
})
</script>
<template>
<div></div>
</template>