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 type { DateCards } from "@/interface/index/Main";
|
||||
import http from "@/plugins/http";
|
||||
|
||||
const $q = useQuasar();
|
||||
|
||||
const cookieTokenName = ref<string>("BMAHRIS_KEYCLOAK_IDENTITY");
|
||||
const cookieTokenRefName = ref<string>("BMAHRIS_KEYCLOAK_REFRESH");
|
||||
const urlAdmin = config.API.URL_ADMIN;
|
||||
const urlUser = config.API.URL_USER;
|
||||
const urlMgt = config.API.URL_MGT;
|
||||
|
|
@ -47,9 +48,10 @@ const cards = ref<DateCards[]>([
|
|||
|
||||
const token = ref<any>("");
|
||||
const refreshToken = ref<any>("");
|
||||
const fullname = computed(() => {
|
||||
if (token.value) {
|
||||
const base64Url = token.value.split(".")[1];
|
||||
const fullname = computed(async () => {
|
||||
const token = await getCookie(cookieTokenName.value);
|
||||
if (token) {
|
||||
const base64Url = token.split(".")[1];
|
||||
|
||||
// แปลงจาก Base64 URL-safe เป็น Base64 ปกติ
|
||||
const base64 = base64Url.replace(/-/g, "+").replace(/_/g, "/");
|
||||
|
|
@ -64,48 +66,52 @@ const fullname = computed(() => {
|
|||
});
|
||||
|
||||
async function goPage(sys: string, url: string) {
|
||||
// แยกส่วน Payload ของ JWT (ส่วนที่ 2)
|
||||
const base64Url = token.value.split(".")[1];
|
||||
const token = await getCookie(cookieTokenName.value);
|
||||
|
||||
// แปลงจาก Base64 URL-safe เป็น Base64 ปกติ
|
||||
const base64 = base64Url.replace(/-/g, "+").replace(/_/g, "/");
|
||||
if (token) {
|
||||
// แยกส่วน Payload ของ JWT (ส่วนที่ 2)
|
||||
const base64Url = token.split(".")[1];
|
||||
|
||||
// ถอดรหัส Base64
|
||||
const decoded = atob(base64);
|
||||
// แปลงจาก Base64 URL-safe เป็น Base64 ปกติ
|
||||
const base64 = base64Url.replace(/-/g, "+").replace(/_/g, "/");
|
||||
|
||||
// กำหนด requiredRole ตามค่าของ sys
|
||||
let requiredRole: string[] = [];
|
||||
// ถอดรหัส Base64
|
||||
const decoded = atob(base64);
|
||||
|
||||
if (sys === "user" || sys === "checkin") {
|
||||
requiredRole = ["USER"];
|
||||
} else if (sys === "mgt") {
|
||||
requiredRole = ["STAFF"]; // ถ้า sys เป็นค่าว่าง ให้ใช้ "ADMIN"
|
||||
} else if (sys === "admin") {
|
||||
requiredRole = ["ADMIN", "SUPER_ADMIN"];
|
||||
}
|
||||
// กำหนด requiredRole ตามค่าของ sys
|
||||
let requiredRole: string[] = [];
|
||||
|
||||
// console.log("requiredRole===>", requiredRole);
|
||||
// console.log("decoded===>", JSON.parse(decoded).realm_access.roles);
|
||||
if (sys === "user" || sys === "checkin") {
|
||||
requiredRole = ["USER"];
|
||||
} else if (sys === "mgt") {
|
||||
requiredRole = ["STAFF"]; // ถ้า sys เป็นค่าว่าง ให้ใช้ "ADMIN"
|
||||
} else if (sys === "admin") {
|
||||
requiredRole = ["ADMIN", "SUPER_ADMIN"];
|
||||
}
|
||||
|
||||
// ตรวจสอบว่า payload.role มีค่าหรือไม่ และว่ามี role ที่ต้องการหรือไม่
|
||||
if (
|
||||
requiredRole.some((role) =>
|
||||
JSON.parse(decoded).realm_access.roles.includes(role)
|
||||
)
|
||||
) {
|
||||
window.location.href = `${url}/auth?token=${token.value}&accessToken=${refreshToken.value}`;
|
||||
} else {
|
||||
// alert("คุณไม่มีสิทธิ์เข้าใช้งานระบบนี้");
|
||||
$q.dialog({
|
||||
component: CustomComponent,
|
||||
componentProps: {
|
||||
title: `แจ้งเตือน`,
|
||||
message: "คุณไม่มีสิทธิ์เข้าใช้งานระบบนี้",
|
||||
icon: "warning",
|
||||
color: "red",
|
||||
onlycancel: true,
|
||||
},
|
||||
});
|
||||
// console.log("requiredRole===>", requiredRole);
|
||||
// console.log("decoded===>", JSON.parse(decoded).realm_access.roles);
|
||||
|
||||
// ตรวจสอบว่า payload.role มีค่าหรือไม่ และว่ามี role ที่ต้องการหรือไม่
|
||||
if (
|
||||
requiredRole.some((role) =>
|
||||
JSON.parse(decoded).realm_access.roles.includes(role)
|
||||
)
|
||||
) {
|
||||
window.location.href = `${url}/auth?token=${token}&accessToken=${refreshToken.value}`;
|
||||
} else {
|
||||
// alert("คุณไม่มีสิทธิ์เข้าใช้งานระบบนี้");
|
||||
$q.dialog({
|
||||
component: CustomComponent,
|
||||
componentProps: {
|
||||
title: `แจ้งเตือน`,
|
||||
message: "คุณไม่มีสิทธิ์เข้าใช้งานระบบนี้",
|
||||
icon: "warning",
|
||||
color: "red",
|
||||
onlycancel: true,
|
||||
},
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -121,8 +127,8 @@ async function logout() {
|
|||
cancel: true,
|
||||
persistent: true,
|
||||
}).onOk(async () => {
|
||||
await deleteCookie("BMAHRIS_KEYCLOAK_IDENTITY");
|
||||
await deleteCookie("BMAHRIS_KEYCLOAK_REFRESH");
|
||||
await deleteCookie(cookieTokenName.value);
|
||||
await deleteCookie(cookieTokenRefName.value);
|
||||
// ยิง logout เข้าระบบ
|
||||
await postSaveLog("ออกจากระบบ", token.value);
|
||||
window.location.href = `${config.API.URL_SSO}`;
|
||||
|
|
@ -151,14 +157,11 @@ async function postSaveLog(type: string, token: any) {
|
|||
}
|
||||
|
||||
onMounted(async () => {
|
||||
token.value = await getCookie("BMAHRIS_KEYCLOAK_IDENTITY");
|
||||
refreshToken.value = await getCookie("BMAHRIS_KEYCLOAK_REFRESH");
|
||||
|
||||
deleteCookie("BMAHRISADM_KEYCLOAK_IDENTITY");
|
||||
deleteCookie("BMAHRISCKI_KEYCLOAK_IDENTITY");
|
||||
deleteCookie("BMAHRISUSER_KEYCLOAK_IDENTITY");
|
||||
|
||||
const checkToken = (await token.value) ?? null;
|
||||
const checkToken = await getCookie(cookieTokenName.value);
|
||||
|
||||
if (!checkToken && !token.value) {
|
||||
await axios
|
||||
|
|
@ -174,8 +177,12 @@ onMounted(async () => {
|
|||
)
|
||||
.then(async (res: any) => {
|
||||
if (res.status === 200) {
|
||||
setCookie("BMAHRIS_KEYCLOAK_IDENTITY", res.data.access_token, 1);
|
||||
setCookie("BMAHRIS_KEYCLOAK_REFRESH", res.data.refresh_token, 1);
|
||||
setCookie(cookieTokenName.value, res.data.access_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 เข้าระบบ
|
||||
await postSaveLog("เข้าสู่ระบบ", res.data.access_token);
|
||||
}
|
||||
|
|
@ -183,6 +190,9 @@ onMounted(async () => {
|
|||
.catch((err: any) => {
|
||||
router.push("/sso");
|
||||
});
|
||||
} else {
|
||||
token.value = await getCookie(cookieTokenName.value);
|
||||
refreshToken.value = await getCookie(cookieTokenRefName.value);
|
||||
}
|
||||
});
|
||||
</script>
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue