updated authen
This commit is contained in:
parent
9ec6a7696a
commit
c1f0ac7939
5 changed files with 39 additions and 28 deletions
38
src/main.ts
38
src/main.ts
|
|
@ -53,24 +53,26 @@ const kcToken = getCookie('BMAHRIS_KEYCLOAK_IDENTITY')
|
||||||
const kcRefreshToken = getCookie('BMAHRIS_KEYCLOAK_REFRESH')
|
const kcRefreshToken = getCookie('BMAHRIS_KEYCLOAK_REFRESH')
|
||||||
|
|
||||||
if (kcToken && kcRefreshToken) {
|
if (kcToken && kcRefreshToken) {
|
||||||
keycloak
|
keycloak.init({
|
||||||
.init({
|
// onLoad: 'login-required',
|
||||||
// onLoad: 'login-required',
|
checkLoginIframe: false,
|
||||||
checkLoginIframe: false,
|
token: kcToken,
|
||||||
token: kcToken,
|
refreshToken: kcRefreshToken,
|
||||||
refreshToken: kcRefreshToken,
|
})
|
||||||
})
|
keycloak.authenticated = true
|
||||||
.then((authenticated) => {
|
// .then((authenticated) => {
|
||||||
console.log('authenticated', authenticated)
|
// console.log('authenticated', authenticated)
|
||||||
if (!authenticated) {
|
// if (!authenticated) {
|
||||||
window.location.reload()
|
// window.location.reload()
|
||||||
} else {
|
// } else {
|
||||||
console.log('Authenticated')
|
// console.log('Authenticated')
|
||||||
}
|
// }
|
||||||
})
|
// })
|
||||||
.catch((err) => {
|
// .catch((err) => {
|
||||||
console.error('Keycloak initialization failed:', err)
|
// console.error('Keycloak initialization failed:', err)
|
||||||
})
|
// })
|
||||||
|
} else {
|
||||||
|
keycloak.authenticated = false
|
||||||
}
|
}
|
||||||
|
|
||||||
app.mount('#app')
|
app.mount('#app')
|
||||||
|
|
|
||||||
|
|
@ -31,6 +31,8 @@ http.interceptors.response.use(
|
||||||
// eslint-disable-next-line no-prototype-builtins
|
// eslint-disable-next-line no-prototype-builtins
|
||||||
if (error.hasOwnProperty('response')) {
|
if (error.hasOwnProperty('response')) {
|
||||||
if (error.response.status === 401 || error.response.status === 403) {
|
if (error.response.status === 401 || error.response.status === 403) {
|
||||||
|
window.location.href = '/login'
|
||||||
|
|
||||||
// Store.commit("SET_ERROR_MESSAGE", error.response.data.message);
|
// Store.commit("SET_ERROR_MESSAGE", error.response.data.message);
|
||||||
// Store.commit("REMOVE_ACCESS_TOKEN")
|
// Store.commit("REMOVE_ACCESS_TOKEN")
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -82,8 +82,11 @@ const router = createRouter({
|
||||||
|
|
||||||
// authen with keycloak client
|
// authen with keycloak client
|
||||||
router.beforeEach((to, from, next) => {
|
router.beforeEach((to, from, next) => {
|
||||||
|
// console.log('keycloak==>', keycloak)
|
||||||
|
console.log('keycloak==>', keycloak.authenticated)
|
||||||
|
|
||||||
if (to.meta.Auth) {
|
if (to.meta.Auth) {
|
||||||
if (keycloak.authenticated === undefined && to.meta.Auth) {
|
if (keycloak.authenticated === false && to.meta.Auth) {
|
||||||
window.location.href = '/login'
|
window.location.href = '/login'
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
|
|
|
||||||
|
|
@ -1,5 +1,5 @@
|
||||||
<script setup lang="ts">
|
<script setup lang="ts">
|
||||||
import { onMounted, ref } from 'vue'
|
import { onMounted } from 'vue'
|
||||||
import { useRoute } from 'vue-router'
|
import { useRoute } from 'vue-router'
|
||||||
|
|
||||||
const route = useRoute()
|
const route = useRoute()
|
||||||
|
|
@ -8,7 +8,7 @@ function setCookie(name: string, value: any, days: number) {
|
||||||
let expires = ''
|
let expires = ''
|
||||||
if (days) {
|
if (days) {
|
||||||
const date = new Date()
|
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()
|
expires = '; expires=' + date.toUTCString()
|
||||||
}
|
}
|
||||||
document.cookie = name + '=' + (value || '') + expires + '; path=/'
|
document.cookie = name + '=' + (value || '') + expires + '; path=/'
|
||||||
|
|
|
||||||
|
|
@ -1,8 +1,8 @@
|
||||||
<!-- authen with keycloak client -->
|
<!-- authen with keycloak client -->
|
||||||
<script setup lang="ts">
|
<script setup lang="ts">
|
||||||
import { ref } from 'vue'
|
import { onMounted, ref } from 'vue'
|
||||||
import axios from 'axios'
|
import axios from 'axios'
|
||||||
import { keycloakConfig } from '@/plugins/keycloak'
|
import keycloak, { keycloakConfig } from '@/plugins/keycloak'
|
||||||
import { useRouter } from 'vue-router'
|
import { useRouter } from 'vue-router'
|
||||||
import { useQuasar } from 'quasar'
|
import { useQuasar } from 'quasar'
|
||||||
import { useCounterMixin } from '@/stores/mixin'
|
import { useCounterMixin } from '@/stores/mixin'
|
||||||
|
|
@ -17,7 +17,7 @@ function setCookie(name: string, value: any, days: number) {
|
||||||
let expires = ''
|
let expires = ''
|
||||||
if (days) {
|
if (days) {
|
||||||
const date = new Date()
|
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()
|
expires = '; expires=' + date.toUTCString()
|
||||||
}
|
}
|
||||||
document.cookie = name + '=' + (value || '') + expires + '; path=/'
|
document.cookie = name + '=' + (value || '') + expires + '; path=/'
|
||||||
|
|
@ -31,10 +31,6 @@ async function onSubmit() {
|
||||||
formdata.append('client_id', keycloakConfig.clientId)
|
formdata.append('client_id', keycloakConfig.clientId)
|
||||||
formdata.append('client_secret', keycloakConfig.clientSecret)
|
formdata.append('client_secret', keycloakConfig.clientSecret)
|
||||||
formdata.append('grant_type', 'password')
|
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('username', username.value)
|
||||||
formdata.append('password', password.value)
|
formdata.append('password', password.value)
|
||||||
|
|
||||||
|
|
@ -68,6 +64,14 @@ async function onSubmit() {
|
||||||
// router.push('/')
|
// router.push('/')
|
||||||
// }
|
// }
|
||||||
}
|
}
|
||||||
|
|
||||||
|
onMounted(() => {
|
||||||
|
if (keycloak.authenticated) {
|
||||||
|
console.log('authenticated', keycloak.authenticated)
|
||||||
|
|
||||||
|
router.push('/')
|
||||||
|
}
|
||||||
|
})
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
<template>
|
<template>
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue