updated authen

This commit is contained in:
Warunee Tamkoo 2024-07-25 11:40:47 +07:00
parent 9ec6a7696a
commit c1f0ac7939
5 changed files with 39 additions and 28 deletions

View file

@ -53,24 +53,26 @@ const kcToken = getCookie('BMAHRIS_KEYCLOAK_IDENTITY')
const kcRefreshToken = getCookie('BMAHRIS_KEYCLOAK_REFRESH')
if (kcToken && kcRefreshToken) {
keycloak
.init({
// onLoad: 'login-required',
checkLoginIframe: false,
token: kcToken,
refreshToken: kcRefreshToken,
})
.then((authenticated) => {
console.log('authenticated', authenticated)
if (!authenticated) {
window.location.reload()
} else {
console.log('Authenticated')
}
})
.catch((err) => {
console.error('Keycloak initialization failed:', err)
})
keycloak.init({
// onLoad: 'login-required',
checkLoginIframe: false,
token: kcToken,
refreshToken: kcRefreshToken,
})
keycloak.authenticated = true
// .then((authenticated) => {
// console.log('authenticated', authenticated)
// if (!authenticated) {
// window.location.reload()
// } else {
// console.log('Authenticated')
// }
// })
// .catch((err) => {
// console.error('Keycloak initialization failed:', err)
// })
} else {
keycloak.authenticated = false
}
app.mount('#app')

View file

@ -31,6 +31,8 @@ http.interceptors.response.use(
// eslint-disable-next-line no-prototype-builtins
if (error.hasOwnProperty('response')) {
if (error.response.status === 401 || error.response.status === 403) {
window.location.href = '/login'
// Store.commit("SET_ERROR_MESSAGE", error.response.data.message);
// Store.commit("REMOVE_ACCESS_TOKEN")
}

View file

@ -82,8 +82,11 @@ const router = createRouter({
// authen with keycloak client
router.beforeEach((to, from, next) => {
// console.log('keycloak==>', keycloak)
console.log('keycloak==>', keycloak.authenticated)
if (to.meta.Auth) {
if (keycloak.authenticated === undefined && to.meta.Auth) {
if (keycloak.authenticated === false && to.meta.Auth) {
window.location.href = '/login'
}
} else {

View file

@ -1,5 +1,5 @@
<script setup lang="ts">
import { onMounted, ref } from 'vue'
import { onMounted } from 'vue'
import { useRoute } from 'vue-router'
const route = useRoute()
@ -8,7 +8,7 @@ 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)
date.setTime(date.getTime() + days * 24 * 60 * 55 * 1000)
expires = '; expires=' + date.toUTCString()
}
document.cookie = name + '=' + (value || '') + expires + '; path=/'

View file

@ -1,8 +1,8 @@
<!-- authen with keycloak client -->
<script setup lang="ts">
import { ref } from 'vue'
import { onMounted, ref } from 'vue'
import axios from 'axios'
import { keycloakConfig } from '@/plugins/keycloak'
import keycloak, { keycloakConfig } from '@/plugins/keycloak'
import { useRouter } from 'vue-router'
import { useQuasar } from 'quasar'
import { useCounterMixin } from '@/stores/mixin'
@ -17,7 +17,7 @@ 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)
date.setTime(date.getTime() + days * 24 * 60 * 55 * 1000)
expires = '; expires=' + date.toUTCString()
}
document.cookie = name + '=' + (value || '') + expires + '; path=/'
@ -31,10 +31,6 @@ async function onSubmit() {
formdata.append('client_id', keycloakConfig.clientId)
formdata.append('client_secret', keycloakConfig.clientSecret)
formdata.append('grant_type', 'password')
formdata.append(
'requested_token_type',
'urn:ietf:params:oauth:token-type:refresh_token'
)
formdata.append('username', username.value)
formdata.append('password', password.value)
@ -68,6 +64,14 @@ async function onSubmit() {
// router.push('/')
// }
}
onMounted(() => {
if (keycloak.authenticated) {
console.log('authenticated', keycloak.authenticated)
router.push('/')
}
})
</script>
<template>