updateAuthen

This commit is contained in:
DESKTOP-1R2VSQH\Lenovo ThinkPad E490 2024-07-25 12:57:27 +07:00
parent 8f103aeb71
commit 02827aa02d
5 changed files with 63 additions and 21 deletions

View file

@ -58,24 +58,27 @@ 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) => {
console.log("authenticated", authenticated); // .then((authenticated) => {
if (!authenticated) { // console.log("authenticated", authenticated);
window.location.reload(); // if (!authenticated) {
} else { // window.location.reload();
console.log("Authenticated"); // } else {
} // console.log("Authenticated");
}) // }
.catch((err) => { // })
console.error("Keycloak initialization failed:", err); // .catch((err) => {
}); // console.error("Keycloak initialization failed:", err);
// });
} else {
keycloak.authenticated = false;
} }
app.mount("#app"); app.mount("#app");

View file

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

View file

@ -78,7 +78,7 @@ const router = createRouter({
// authen with keycloak client // authen with keycloak client
router.beforeEach((to, from, next) => { router.beforeEach((to, from, next) => {
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 {

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