check role

This commit is contained in:
Warunee Tamkoo 2024-08-19 10:49:09 +07:00
parent 2a1e85f720
commit 28c91a3a81
3 changed files with 35 additions and 17 deletions

View file

@ -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() {

View file

@ -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);

View file

@ -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<boolean>(true); // check display login page
const username = ref<string>("");
const password = ref<string>("");
/**
* @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("/");
}
}
}
});
</script>
<template>
<div class="bg-image">
<div v-if="isDisplay" class="bg-image">
<div class="login-pf-page">
<div id="kc-logo">
<div id="myimage"></div>