check role

This commit is contained in:
Warunee Tamkoo 2024-08-19 11:19:20 +07:00
parent 9f4aeec575
commit b9b8bac836
3 changed files with 31 additions and 9 deletions

View file

@ -104,9 +104,4 @@ if (auth.token && auth.refresh_token) {
// });
}
app.config.globalProperties.$checkPermission = function () {
console.log("This is a checkPermission");
return true;
};
app.mount("#app");

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

@ -6,6 +6,7 @@ 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();
@ -15,6 +16,10 @@ const { showLoader, hideLoader, messageError } = mixin;
const username = ref<string>("");
const password = ref<string>("");
const isDisplay = ref<boolean>(true); // check display login page
/**
* @description งกนเขาสระบบ
*/
async function onSubmit() {
showLoader();
const formdata = new URLSearchParams();
@ -50,16 +55,38 @@ async function onSubmit() {
}
onMounted(() => {
// check authen keycloak and role of system
if (keycloak.authenticated) {
console.log("authenticated", keycloak.authenticated);
isDisplay.value = false;
showLoader();
if (keycloak.tokenParsed) {
const checkRole = (element: string) => element === "STAFF";
router.push("/");
//
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>