From 55756ef9c30c7538e8c11f80f3fc04648c4ba987 Mon Sep 17 00:00:00 2001 From: "DESKTOP-1R2VSQH\\Lenovo ThinkPad E490" Date: Thu, 27 Mar 2025 16:54:04 +0700 Subject: [PATCH] cookie sso --- src/plugins/auth.ts | 5 +++-- src/views/MainLayout.vue | 8 ++++++-- src/views/auth.vue | 2 +- src/views/login.vue | 2 +- 4 files changed, 11 insertions(+), 6 deletions(-) diff --git a/src/plugins/auth.ts b/src/plugins/auth.ts index d8f0e94f..9d95e775 100644 --- a/src/plugins/auth.ts +++ b/src/plugins/auth.ts @@ -10,8 +10,9 @@ interface AuthResponse { const authenticated = async () => ((await getToken()) ? true : false); -async function setAuthen(r: AuthResponse) { +async function setAuthen(r: AuthResponse, val: string) { await setCookie(ACCESS_TOKEN, r.access_token, r.expires_in); + setCookie("SSO", val, r.expires_in); window.location.href = "/"; } @@ -70,4 +71,4 @@ async function tokenParsed() { return JSON.parse(jsonPayload); } -export { getToken, authenticated, logout, setAuthen, tokenParsed }; +export { getToken, authenticated, logout, setAuthen, tokenParsed, getCookie }; diff --git a/src/views/MainLayout.vue b/src/views/MainLayout.vue index f22ca37c..87386a4a 100644 --- a/src/views/MainLayout.vue +++ b/src/views/MainLayout.vue @@ -6,7 +6,7 @@ import { useQuasar } from "quasar"; import { useRoute } from "vue-router"; import config from "@/app.config"; import http from "@/plugins/http"; -import { logout, tokenParsed } from "@/plugins/auth"; +import { logout, tokenParsed, getCookie } from "@/plugins/auth"; import checkPermission from "@/plugins/checkPermission"; import { useCounterMixin } from "@/stores/mixin"; import { useDataStore } from "@/stores/data"; @@ -372,6 +372,8 @@ watch( } ); +const isSsoToken = ref(false); + /** * ฮุกนี้จะถูกเรียกก่อนที่คอมโพเนนต์จะถูก onMounted * กำหนดบทบาทและชื่อผู้ใช้งานจาก Keycloak @@ -381,6 +383,8 @@ onBeforeMount(async () => { // ดิงชื่อผู้ใช้งานจาก keycloak const tokenParsedData: any = await tokenParsed(); + const SSO_TOKEN = await getCookie("SSO"); + isSsoToken.value = SSO_TOKEN === "y" ? true : false; if (tokenParsedData != null) { fullname.value = tokenParsedData.name; role.value = tokenParsedData.role; @@ -623,7 +627,7 @@ onUnmounted(() => { --> - + { expires_in: route.query.expires ? route.query.expires : 36000, refresh_token: route.query.accessToken, }; - setAuthen(params); + setAuthen(params, "y"); } }); diff --git a/src/views/login.vue b/src/views/login.vue index 8e5e8548..06fe7bc2 100644 --- a/src/views/login.vue +++ b/src/views/login.vue @@ -36,7 +36,7 @@ async function onSubmit() { }, }) .then((res) => { - setAuthen(res.data.result); + setAuthen(res.data.result, "n"); }) .catch((err) => { $q.dialog({