fixing cookie

This commit is contained in:
Warunee Tamkoo 2024-12-19 09:58:50 +07:00
parent 3f5b7356f1
commit 71d85cf2bb

View file

@ -13,10 +13,11 @@ import screen3 from "@/assets/screen3.png";
import screen4 from "@/assets/screen4.png"; import screen4 from "@/assets/screen4.png";
import type { DateCards } from "@/interface/index/Main"; import type { DateCards } from "@/interface/index/Main";
import http from "@/plugins/http";
const $q = useQuasar(); const $q = useQuasar();
const cookieTokenName = ref<string>("BMAHRIS_KEYCLOAK_IDENTITY");
const cookieTokenRefName = ref<string>("BMAHRIS_KEYCLOAK_REFRESH");
const urlAdmin = config.API.URL_ADMIN; const urlAdmin = config.API.URL_ADMIN;
const urlUser = config.API.URL_USER; const urlUser = config.API.URL_USER;
const urlMgt = config.API.URL_MGT; const urlMgt = config.API.URL_MGT;
@ -47,9 +48,10 @@ const cards = ref<DateCards[]>([
const token = ref<any>(""); const token = ref<any>("");
const refreshToken = ref<any>(""); const refreshToken = ref<any>("");
const fullname = computed(() => { const fullname = computed(async () => {
if (token.value) { const token = await getCookie(cookieTokenName.value);
const base64Url = token.value.split(".")[1]; if (token) {
const base64Url = token.split(".")[1];
// Base64 URL-safe Base64 // Base64 URL-safe Base64
const base64 = base64Url.replace(/-/g, "+").replace(/_/g, "/"); const base64 = base64Url.replace(/-/g, "+").replace(/_/g, "/");
@ -64,8 +66,11 @@ const fullname = computed(() => {
}); });
async function goPage(sys: string, url: string) { async function goPage(sys: string, url: string) {
const token = await getCookie(cookieTokenName.value);
if (token) {
// Payload JWT ( 2) // Payload JWT ( 2)
const base64Url = token.value.split(".")[1]; const base64Url = token.split(".")[1];
// Base64 URL-safe Base64 // Base64 URL-safe Base64
const base64 = base64Url.replace(/-/g, "+").replace(/_/g, "/"); const base64 = base64Url.replace(/-/g, "+").replace(/_/g, "/");
@ -93,7 +98,7 @@ async function goPage(sys: string, url: string) {
JSON.parse(decoded).realm_access.roles.includes(role) JSON.parse(decoded).realm_access.roles.includes(role)
) )
) { ) {
window.location.href = `${url}/auth?token=${token.value}&accessToken=${refreshToken.value}`; window.location.href = `${url}/auth?token=${token}&accessToken=${refreshToken.value}`;
} else { } else {
// alert(""); // alert("");
$q.dialog({ $q.dialog({
@ -108,6 +113,7 @@ async function goPage(sys: string, url: string) {
}); });
} }
} }
}
async function logout() { async function logout() {
$q.dialog({ $q.dialog({
@ -121,8 +127,8 @@ async function logout() {
cancel: true, cancel: true,
persistent: true, persistent: true,
}).onOk(async () => { }).onOk(async () => {
await deleteCookie("BMAHRIS_KEYCLOAK_IDENTITY"); await deleteCookie(cookieTokenName.value);
await deleteCookie("BMAHRIS_KEYCLOAK_REFRESH"); await deleteCookie(cookieTokenRefName.value);
// logout // logout
await postSaveLog("ออกจากระบบ", token.value); await postSaveLog("ออกจากระบบ", token.value);
window.location.href = `${config.API.URL_SSO}`; window.location.href = `${config.API.URL_SSO}`;
@ -151,14 +157,11 @@ async function postSaveLog(type: string, token: any) {
} }
onMounted(async () => { onMounted(async () => {
token.value = await getCookie("BMAHRIS_KEYCLOAK_IDENTITY");
refreshToken.value = await getCookie("BMAHRIS_KEYCLOAK_REFRESH");
deleteCookie("BMAHRISADM_KEYCLOAK_IDENTITY"); deleteCookie("BMAHRISADM_KEYCLOAK_IDENTITY");
deleteCookie("BMAHRISCKI_KEYCLOAK_IDENTITY"); deleteCookie("BMAHRISCKI_KEYCLOAK_IDENTITY");
deleteCookie("BMAHRISUSER_KEYCLOAK_IDENTITY"); deleteCookie("BMAHRISUSER_KEYCLOAK_IDENTITY");
const checkToken = (await token.value) ?? null; const checkToken = await getCookie(cookieTokenName.value);
if (!checkToken && !token.value) { if (!checkToken && !token.value) {
await axios await axios
@ -174,8 +177,12 @@ onMounted(async () => {
) )
.then(async (res: any) => { .then(async (res: any) => {
if (res.status === 200) { if (res.status === 200) {
setCookie("BMAHRIS_KEYCLOAK_IDENTITY", res.data.access_token, 1); setCookie(cookieTokenName.value, res.data.access_token, 1);
setCookie("BMAHRIS_KEYCLOAK_REFRESH", res.data.refresh_token, 1); setCookie(cookieTokenRefName.value, res.data.refresh_token, 1);
token.value = await res.data.access_token;
refreshToken.value = await res.data.refresh_token;
// log // log
await postSaveLog("เข้าสู่ระบบ", res.data.access_token); await postSaveLog("เข้าสู่ระบบ", res.data.access_token);
} }
@ -183,6 +190,9 @@ onMounted(async () => {
.catch((err: any) => { .catch((err: any) => {
router.push("/sso"); router.push("/sso");
}); });
} else {
token.value = await getCookie(cookieTokenName.value);
refreshToken.value = await getCookie(cookieTokenRefName.value);
} }
}); });
</script> </script>