diff --git a/src/plugins/keycloak.ts b/src/plugins/keycloak.ts index b1ac26ca..52bd62db 100644 --- a/src/plugins/keycloak.ts +++ b/src/plugins/keycloak.ts @@ -15,7 +15,7 @@ const keycloak = new Keycloak(keycloakConfig); async function kcAuthen(access_token: string, refresh_token: string) { await setCookie(ACCESS_TOKEN, access_token, 1); await setCookie(REFRESH_TOKEN, refresh_token, 1); - window.location.href = "/"; + window.location.href = "/login"; } async function kcLogout() { diff --git a/src/views/MainLayout.vue b/src/views/MainLayout.vue index 355a7e87..ad925960 100644 --- a/src/views/MainLayout.vue +++ b/src/views/MainLayout.vue @@ -17,6 +17,7 @@ import type { } from "../interface/request/main/main"; import { menuList } from "../interface/request/main/main"; import checkPermission from "@/plugins/checkPermission"; + // import { useroleUserDataStore } from "@/stores/roleUser"; const { setVerticalScrollPosition } = scroll; @@ -190,9 +191,6 @@ const activeBtn = () => { * ยังจับ boolean ผิด จึงต้อง set */ onMounted(async () => { - // if (keycloak.tokenParsed) { - // await fetchroleUser(keycloak.tokenParsed.role); - // } await fetchmsgNoread(); // await getDataNotification(1, "NOMAL"); myEventHandler(null, false); diff --git a/src/views/login.vue b/src/views/login.vue index 81725ef3..d5aaa763 100644 --- a/src/views/login.vue +++ b/src/views/login.vue @@ -6,25 +6,21 @@ import keycloak, { keycloakConfig, kcAuthen } from "@/plugins/keycloak"; import { useRouter } from "vue-router"; import { useQuasar } from "quasar"; import { useCounterMixin } from "@/stores/mixin"; +import CustomComponent from "@/components/CustomDialog.vue"; const router = useRouter(); const mixin = useCounterMixin(); const $q = useQuasar(); //ใช้ noti quasar const { showLoader, hideLoader, messageError } = mixin; - -function setCookie(name: string, value: any, days: number) { - let expires = ""; - if (days) { - const date = new Date(); - date.setTime(date.getTime() + days * 24 * 60 * 60 * 1000); - expires = "; expires=" + date.toUTCString(); - } - document.cookie = name + "=" + (value || "") + expires + "; path=/"; -} +const isDisplay = ref(true); // check display login page const username = ref(""); const password = ref(""); + +/** + * @description ฟังก์ชั่นเข้าสู่ระบบ + */ async function onSubmit() { showLoader(); const formdata = new URLSearchParams(); @@ -60,15 +56,39 @@ async function onSubmit() { } onMounted(() => { + // check authen keycloak and role of system if (keycloak.authenticated) { - console.log("authenticated", keycloak.authenticated); - router.push("/"); + isDisplay.value = false; + showLoader(); + if (keycloak.tokenParsed) { + const checkRole = (element: string) => + element === "ADMIN" || element === "SUPER_ADMIN"; + + // ถ้าไม่มีสิทธิ์เข้าใช้งานระบบ แสดงข้อความแจ้งเตือน + if (keycloak.tokenParsed.role.findIndex(checkRole) === -1) { + $q.dialog({ + component: CustomComponent, + componentProps: { + title: "ข้อความแจ้งเตือน", + message: "ขออภัยคุณไม่มีสิทธิ์เข้าใช้งานระบบนี้", + icon: "warning", + color: "red", + onlycancel: true, + }, + }); + hideLoader(); + isDisplay.value = true; + } else { + // ถ้ามีสิทธิ์เข้าใช้งานระบบ พาไปหน้าหลัก + router.push("/"); + } + } } });