From f6b2b2699880bc7a37003cb1ab512c1fd71e8edf Mon Sep 17 00:00:00 2001 From: waruneeauy Date: Fri, 26 Jul 2024 10:19:35 +0700 Subject: [PATCH] fixing check token --- src/main.ts | 3 --- src/plugins/http.ts | 4 ++-- src/plugins/keycloak.ts | 14 +++++++++++++- src/router/index.ts | 2 +- src/views/MainLayout.vue | 31 +++++++++++++++++-------------- 5 files changed, 33 insertions(+), 21 deletions(-) diff --git a/src/main.ts b/src/main.ts index d0cbe60..2d61f56 100644 --- a/src/main.ts +++ b/src/main.ts @@ -64,7 +64,6 @@ if (kcToken && kcRefreshToken) { token: kcToken, refreshToken: kcRefreshToken, }); - keycloak.authenticated = true; // .then((authenticated) => { // console.log("authenticated", authenticated); @@ -77,8 +76,6 @@ if (kcToken && kcRefreshToken) { // .catch((err) => { // console.error("Keycloak initialization failed:", err); // }); -} else { - keycloak.authenticated = false; } app.mount("#app"); diff --git a/src/plugins/http.ts b/src/plugins/http.ts index 8aebceb..e278860 100644 --- a/src/plugins/http.ts +++ b/src/plugins/http.ts @@ -1,5 +1,5 @@ import Axios, { type AxiosRequestConfig, type AxiosResponse } from "axios"; -import keycloak from "./keycloak"; +import keycloak, { kcLogout } from "./keycloak"; const http = Axios.create({ timeout: 1000000000, // เพิ่มค่า timeout @@ -33,7 +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"; + kcLogout(); // Store.commit("SET_ERROR_MESSAGE", error.response.data.message); // Store.commit("REMOVE_ACCESS_TOKEN") diff --git a/src/plugins/keycloak.ts b/src/plugins/keycloak.ts index 214c25e..a0e8cd4 100644 --- a/src/plugins/keycloak.ts +++ b/src/plugins/keycloak.ts @@ -9,5 +9,17 @@ const keycloakConfig = { }; const keycloak = new Keycloak(keycloakConfig); + +async function kcLogout() { + await deleteCookie("BMAHRIS_KEYCLOAK_IDENTITY"); + await deleteCookie("BMAHRIS_KEYCLOAK_REFRESH"); + keycloak.logout(); + window.location.href = "/login"; +} + +function deleteCookie(name: string) { + document.cookie = `${name}=; expires=Thu, 01 Jan 1970 00:00:00 UTC; path=/;`; +} + export default keycloak; -export { keycloakConfig }; +export { keycloakConfig, kcLogout }; diff --git a/src/router/index.ts b/src/router/index.ts index 32d2156..ad5dd66 100644 --- a/src/router/index.ts +++ b/src/router/index.ts @@ -78,7 +78,7 @@ const router = createRouter({ // authen with keycloak client router.beforeEach((to, from, next) => { if (to.meta.Auth) { - if (keycloak.authenticated === false && to.meta.Auth) { + if (keycloak.authenticated === undefined && to.meta.Auth) { window.location.href = "/login"; } } else { diff --git a/src/views/MainLayout.vue b/src/views/MainLayout.vue index ddba03d..094be6a 100644 --- a/src/views/MainLayout.vue +++ b/src/views/MainLayout.vue @@ -4,7 +4,7 @@ import config from "@/app.config"; import { onMounted, ref, watch } from "vue"; import { useRoute, useRouter } from "vue-router"; import { useQuasar } from "quasar"; -import keycloak from "@/plugins/keycloak"; +import keycloak, { kcLogout } from "@/plugins/keycloak"; import { useCounterMixin } from "@/stores/mixin"; const route = useRoute(); @@ -93,20 +93,13 @@ const doLogout = () => { $q, async () => { // authen with keycloak client - keycloak.logout(); - await deleteCookie("BMAHRIS_KEYCLOAK_IDENTITY"); - await deleteCookie("BMAHRIS_KEYCLOAK_REFRESH"); - window.location.href = "/login"; + kcLogout(); }, "ยืนยันการออกจากระบบ", "ต้องการออกจากระบบใช่หรือไม่" ); }; -function deleteCookie(name: string) { - document.cookie = `${name}=; expires=Thu, 01 Jan 1970 00:00:00 UTC; path=/;`; -} - const clickDelete = async (id: string, index: number) => { dialogRemove($q, async () => { // showLoader(); @@ -458,19 +451,29 @@ function onInfo() { >ขอลาออก - + - + เปลี่ยนรหัสผ่านเปลี่ยนรหัสผ่าน - + - + ออกจากระบบ