fixing cookie
This commit is contained in:
parent
3f5b7356f1
commit
71d85cf2bb
1 changed files with 58 additions and 48 deletions
|
|
@ -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,48 +66,52 @@ const fullname = computed(() => {
|
||||||
});
|
});
|
||||||
|
|
||||||
async function goPage(sys: string, url: string) {
|
async function goPage(sys: string, url: string) {
|
||||||
// แยกส่วน Payload ของ JWT (ส่วนที่ 2)
|
const token = await getCookie(cookieTokenName.value);
|
||||||
const base64Url = token.value.split(".")[1];
|
|
||||||
|
|
||||||
// แปลงจาก Base64 URL-safe เป็น Base64 ปกติ
|
if (token) {
|
||||||
const base64 = base64Url.replace(/-/g, "+").replace(/_/g, "/");
|
// แยกส่วน Payload ของ JWT (ส่วนที่ 2)
|
||||||
|
const base64Url = token.split(".")[1];
|
||||||
|
|
||||||
// ถอดรหัส Base64
|
// แปลงจาก Base64 URL-safe เป็น Base64 ปกติ
|
||||||
const decoded = atob(base64);
|
const base64 = base64Url.replace(/-/g, "+").replace(/_/g, "/");
|
||||||
|
|
||||||
// กำหนด requiredRole ตามค่าของ sys
|
// ถอดรหัส Base64
|
||||||
let requiredRole: string[] = [];
|
const decoded = atob(base64);
|
||||||
|
|
||||||
if (sys === "user" || sys === "checkin") {
|
// กำหนด requiredRole ตามค่าของ sys
|
||||||
requiredRole = ["USER"];
|
let requiredRole: string[] = [];
|
||||||
} else if (sys === "mgt") {
|
|
||||||
requiredRole = ["STAFF"]; // ถ้า sys เป็นค่าว่าง ให้ใช้ "ADMIN"
|
|
||||||
} else if (sys === "admin") {
|
|
||||||
requiredRole = ["ADMIN", "SUPER_ADMIN"];
|
|
||||||
}
|
|
||||||
|
|
||||||
// console.log("requiredRole===>", requiredRole);
|
if (sys === "user" || sys === "checkin") {
|
||||||
// console.log("decoded===>", JSON.parse(decoded).realm_access.roles);
|
requiredRole = ["USER"];
|
||||||
|
} else if (sys === "mgt") {
|
||||||
|
requiredRole = ["STAFF"]; // ถ้า sys เป็นค่าว่าง ให้ใช้ "ADMIN"
|
||||||
|
} else if (sys === "admin") {
|
||||||
|
requiredRole = ["ADMIN", "SUPER_ADMIN"];
|
||||||
|
}
|
||||||
|
|
||||||
// ตรวจสอบว่า payload.role มีค่าหรือไม่ และว่ามี role ที่ต้องการหรือไม่
|
// console.log("requiredRole===>", requiredRole);
|
||||||
if (
|
// console.log("decoded===>", JSON.parse(decoded).realm_access.roles);
|
||||||
requiredRole.some((role) =>
|
|
||||||
JSON.parse(decoded).realm_access.roles.includes(role)
|
// ตรวจสอบว่า payload.role มีค่าหรือไม่ และว่ามี role ที่ต้องการหรือไม่
|
||||||
)
|
if (
|
||||||
) {
|
requiredRole.some((role) =>
|
||||||
window.location.href = `${url}/auth?token=${token.value}&accessToken=${refreshToken.value}`;
|
JSON.parse(decoded).realm_access.roles.includes(role)
|
||||||
} else {
|
)
|
||||||
// alert("คุณไม่มีสิทธิ์เข้าใช้งานระบบนี้");
|
) {
|
||||||
$q.dialog({
|
window.location.href = `${url}/auth?token=${token}&accessToken=${refreshToken.value}`;
|
||||||
component: CustomComponent,
|
} else {
|
||||||
componentProps: {
|
// alert("คุณไม่มีสิทธิ์เข้าใช้งานระบบนี้");
|
||||||
title: `แจ้งเตือน`,
|
$q.dialog({
|
||||||
message: "คุณไม่มีสิทธิ์เข้าใช้งานระบบนี้",
|
component: CustomComponent,
|
||||||
icon: "warning",
|
componentProps: {
|
||||||
color: "red",
|
title: `แจ้งเตือน`,
|
||||||
onlycancel: true,
|
message: "คุณไม่มีสิทธิ์เข้าใช้งานระบบนี้",
|
||||||
},
|
icon: "warning",
|
||||||
});
|
color: "red",
|
||||||
|
onlycancel: true,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -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>
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue