updateAuthen

This commit is contained in:
DESKTOP-1R2VSQH\Lenovo ThinkPad E490 2024-07-25 12:57:05 +07:00
parent ff93b49bc0
commit ba933b54f9
5 changed files with 60 additions and 21 deletions

View file

@ -104,24 +104,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

@ -33,6 +33,7 @@ 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

@ -74,7 +74,7 @@ const router = createRouter({
// authen with keycloak client
router.beforeEach((to, from, next) => {
if (to.meta.Auth) {
if (keycloak.authenticated === undefined && to.meta.Auth) {
if (keycloak.authenticated === false && to.meta.Auth) {
window.location.href = "/login";
}
} else {

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

@ -0,0 +1,30 @@
<script setup lang="ts">
import { onMounted } 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 * 55 * 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>

View file

@ -1,8 +1,8 @@
<!-- authen with keycloak client -->
<script setup lang="ts">
import { ref } from "vue";
import { ref, onMounted } 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";
@ -68,6 +68,12 @@ async function onSubmit() {
// router.push('/')
// }
}
onMounted(() => {
if (keycloak.authenticated) {
router.push("/");
}
});
</script>
<template>