Merge branch 'develop' into devTee
This commit is contained in:
commit
c9ca72f5b1
9 changed files with 92 additions and 159 deletions
27
src/main.ts
27
src/main.ts
|
|
@ -3,7 +3,7 @@ import App from "./App.vue";
|
|||
import router from "./router";
|
||||
import { Dialog, Notify, Quasar, Loading } from "quasar";
|
||||
import "./quasar-user-options";
|
||||
import keycloak from "@/plugins/keycloak";
|
||||
import keycloak, { getToken } from "@/plugins/keycloak";
|
||||
|
||||
import qDraggableTable from "quasar-ui-q-draggable-table";
|
||||
import "quasar-ui-q-draggable-table/dist/index.css";
|
||||
|
|
@ -83,29 +83,14 @@ app.component(
|
|||
app.config.globalProperties.$http = http;
|
||||
|
||||
// authen with keycloak client
|
||||
function getCookie(name: string) {
|
||||
const nameEQ = name + "=";
|
||||
const ca = document.cookie.split(";");
|
||||
for (let i = 0; i < ca.length; i++) {
|
||||
let c = ca[i];
|
||||
while (c.charAt(0) == " ") c = c.substring(1, c.length);
|
||||
if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length, c.length);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
const auth = await getToken();
|
||||
|
||||
const kcToken = getCookie("BMAHRIS_KEYCLOAK_IDENTITY");
|
||||
const kcRefreshToken = getCookie("BMAHRIS_KEYCLOAK_REFRESH");
|
||||
|
||||
if (kcToken && kcRefreshToken) {
|
||||
if (auth.token && auth.refresh_token) {
|
||||
keycloak.init({
|
||||
// onLoad: 'login-required',
|
||||
checkLoginIframe: false,
|
||||
token: kcToken,
|
||||
refreshToken: kcRefreshToken,
|
||||
token: auth.token,
|
||||
refreshToken: auth.refresh_token,
|
||||
});
|
||||
keycloak.authenticated = true;
|
||||
|
||||
// .then((authenticated) => {
|
||||
// console.log("authenticated", authenticated);
|
||||
// if (!authenticated) {
|
||||
|
|
@ -117,8 +102,6 @@ if (kcToken && kcRefreshToken) {
|
|||
// .catch((err) => {
|
||||
// console.error("Keycloak initialization failed:", err);
|
||||
// });
|
||||
} else {
|
||||
keycloak.authenticated = false;
|
||||
}
|
||||
|
||||
app.mount("#app");
|
||||
|
|
|
|||
|
|
@ -2,8 +2,6 @@ const supportMain = () => import("@/modules/00_support/views/MainPage.vue");
|
|||
const supportCategory = () =>
|
||||
import("@/modules/00_support/views/ManageCategory.vue");
|
||||
|
||||
// const testView = () => import("@/views/TestManagement.vue");
|
||||
|
||||
export default [
|
||||
{
|
||||
path: "/support",
|
||||
|
|
@ -25,15 +23,4 @@ export default [
|
|||
Role: "support",
|
||||
},
|
||||
},
|
||||
|
||||
// {
|
||||
// path: "/test",
|
||||
// name: "testView",
|
||||
// component: testView,
|
||||
// meta: {
|
||||
// Auth: true,
|
||||
// Key: [1.1],
|
||||
// Role: "support",
|
||||
// },
|
||||
// },
|
||||
];
|
||||
|
|
|
|||
|
|
@ -1,5 +1,5 @@
|
|||
import Axios, { type AxiosRequestConfig, type AxiosResponse } from "axios";
|
||||
import keycloak from "./keycloak";
|
||||
import keycloak, { kcLogout } from "./keycloak";
|
||||
|
||||
const http = Axios.create({
|
||||
timeout: 1000000000, // เพิ่มค่า timeout
|
||||
|
|
@ -33,7 +33,7 @@ http.interceptors.response.use(
|
|||
// eslint-disable-next-line no-prototype-builtins
|
||||
if (error.hasOwnProperty("response")) {
|
||||
if (error.response.status === 401 || error.response.status === 403) {
|
||||
window.location.href = "/login";
|
||||
kcLogout();
|
||||
|
||||
// Store.commit("SET_ERROR_MESSAGE", error.response.data.message);
|
||||
// Store.commit("REMOVE_ACCESS_TOKEN")
|
||||
|
|
|
|||
|
|
@ -1,30 +1,8 @@
|
|||
// /**
|
||||
// * front connect to keycloak
|
||||
// */
|
||||
// import Keycloak from "keycloak-js";
|
||||
// // import config from "../app.config";
|
||||
// // import http from "../shared/http";
|
||||
// // import router from "../router";
|
||||
|
||||
// const initOptions = {
|
||||
// realm: import.meta.env.VITE_REALM_KEYCLOAK,
|
||||
// clientId: import.meta.env.VITE_CLIENTID_KEYCLOAK,
|
||||
// url: import.meta.env.VITE_URL_KEYCLOAK,
|
||||
// // realm: "bma-ehr",
|
||||
// // clientId: "bma-ehr-vue3",
|
||||
// // url: "https://id.frappet.synology.me/",
|
||||
// }; //option keycloak ที่จะ connect
|
||||
|
||||
// const keycloak = Keycloak(initOptions);
|
||||
|
||||
// keycloak.onAuthSuccess = () => {}; //เพิ่มlogin สำเร็จจะมาทำฟังก์ชันนี้
|
||||
|
||||
// await keycloak.init({ onLoad: "check-sso", checkLoginIframe: false }); //ทำการ connect keycloak
|
||||
// export default keycloak;
|
||||
|
||||
// authen with keycloak client
|
||||
import Keycloak from "keycloak-js";
|
||||
|
||||
const ACCESS_TOKEN = "BMAHRIS_KEYCLOAK_IDENTITY";
|
||||
const REFRESH_TOKEN = "BMAHRIS_KEYCLOAK_REFRESH";
|
||||
const keycloakConfig = {
|
||||
url: "https://id.frappet.synology.me",
|
||||
realm: "bma-ehr",
|
||||
|
|
@ -33,5 +11,60 @@ const keycloakConfig = {
|
|||
};
|
||||
|
||||
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 = "/";
|
||||
}
|
||||
|
||||
async function kcLogout() {
|
||||
await deleteCookie(ACCESS_TOKEN);
|
||||
await deleteCookie(REFRESH_TOKEN);
|
||||
if (keycloak.authenticated !== undefined) {
|
||||
keycloak.logout();
|
||||
}
|
||||
window.location.href = "/login";
|
||||
}
|
||||
|
||||
async function getToken() {
|
||||
return {
|
||||
token: getCookie(ACCESS_TOKEN),
|
||||
refresh_token: getCookie(REFRESH_TOKEN),
|
||||
};
|
||||
}
|
||||
|
||||
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=/";
|
||||
}
|
||||
|
||||
function getCookie(name: string) {
|
||||
const nameEQ = name + "=";
|
||||
const ca = document.cookie.split(";");
|
||||
for (let i = 0; i < ca.length; i++) {
|
||||
let c = ca[i];
|
||||
while (c.charAt(0) == " ") c = c.substring(1, c.length);
|
||||
if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length, c.length);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
function deleteCookie(name: string) {
|
||||
document.cookie = `${name}=; expires=Thu, 01 Jan 1970 00:00:00 UTC; path=/;`;
|
||||
}
|
||||
|
||||
export default keycloak;
|
||||
export { keycloakConfig };
|
||||
export {
|
||||
keycloakConfig,
|
||||
getToken,
|
||||
kcAuthen,
|
||||
kcLogout,
|
||||
ACCESS_TOKEN,
|
||||
REFRESH_TOKEN,
|
||||
};
|
||||
|
|
|
|||
|
|
@ -28,7 +28,7 @@ import ModuleSupport from "@/modules/00_support/router";
|
|||
import ModuleActing from "@/modules/17_acting/router";
|
||||
|
||||
// TODO: ใช้หรือไม่?
|
||||
import keycloak from "@/plugins/keycloak";
|
||||
import keycloak, { kcLogout } from "@/plugins/keycloak";
|
||||
|
||||
const router = createRouter({
|
||||
history: createWebHistory(import.meta.env.BASE_URL),
|
||||
|
|
@ -88,9 +88,13 @@ const router = createRouter({
|
|||
component: loginView,
|
||||
meta: {
|
||||
Auth: false,
|
||||
Key: [7],
|
||||
},
|
||||
},
|
||||
{
|
||||
path: "/auth",
|
||||
name: "auth",
|
||||
component: () => import("@/views/auth.vue"),
|
||||
},
|
||||
],
|
||||
|
||||
scrollBehavior(to, from, savedPosition) {
|
||||
|
|
@ -108,8 +112,10 @@ const router = createRouter({
|
|||
// authen with keycloak client
|
||||
router.beforeEach((to, from, next) => {
|
||||
if (to.meta.Auth) {
|
||||
if (keycloak.authenticated === false && to.meta.Auth) {
|
||||
window.location.href = "/login";
|
||||
if (keycloak.authenticated === undefined) {
|
||||
kcLogout();
|
||||
} else {
|
||||
next();
|
||||
}
|
||||
} else {
|
||||
next();
|
||||
|
|
|
|||
|
|
@ -1,6 +1,6 @@
|
|||
<script setup lang="ts">
|
||||
import { ref, onMounted, onUnmounted, watch } from "vue";
|
||||
import keycloak from "@/plugins/keycloak";
|
||||
import keycloak, { kcLogout } from "@/plugins/keycloak";
|
||||
import { useRoute, useRouter } from "vue-router";
|
||||
import { useDataStore } from "@/stores/data";
|
||||
import { storeToRefs } from "pinia";
|
||||
|
|
@ -416,20 +416,13 @@ const doLogout = () => {
|
|||
dialogConfirm(
|
||||
$q,
|
||||
async () => {
|
||||
await deleteCookie("BMAHRIS_KEYCLOAK_IDENTITY");
|
||||
await deleteCookie("BMAHRIS_KEYCLOAK_REFRESH");
|
||||
window.location.href = "/login";
|
||||
kcLogout();
|
||||
},
|
||||
// keycloak.logout(),
|
||||
"ยืนยันการออกจากระบบ",
|
||||
"ต้องการออกจากระบบใช่หรือไม่?"
|
||||
);
|
||||
};
|
||||
|
||||
function deleteCookie(name: string) {
|
||||
document.cookie = `${name}=; expires=Thu, 01 Jan 1970 00:00:00 UTC; path=/;`;
|
||||
}
|
||||
|
||||
/**
|
||||
* ดิงชื่อผู้ใช้งานจาก keycloak
|
||||
*/
|
||||
|
|
@ -527,8 +520,7 @@ function fetchPermissionsSys() {
|
|||
http
|
||||
.get(config.API.orgPermissionsSys)
|
||||
.then((res) => {
|
||||
|
||||
storeMenu.fetchDataPermission(res.data.result)
|
||||
storeMenu.fetchDataPermission(res.data.result);
|
||||
})
|
||||
.catch((err) => {
|
||||
messageError($q, err);
|
||||
|
|
|
|||
|
|
@ -1,38 +0,0 @@
|
|||
<script setup lang="ts">
|
||||
function deleteCookie(name: string, path: string = "/", domain?: string) {
|
||||
let cookieString = `${name}=; expires=Thu, 01 Jan 1970 00:00:00 UTC; path=${path};`;
|
||||
if (domain) {
|
||||
cookieString += ` domain=${domain};`;
|
||||
}
|
||||
document.cookie = cookieString;
|
||||
console.log("Deleted cookie:", name);
|
||||
}
|
||||
|
||||
function showCookies() {
|
||||
const cookies = document.cookie.split(";").map((cookie) => cookie.trim());
|
||||
const cookieNames = cookies.map((cookie) => cookie.split("=")[0]);
|
||||
console.log("Cookies:", cookieNames);
|
||||
}
|
||||
|
||||
function handleDeleteCookie() {
|
||||
deleteCookie("AUTH_SESSION_ID");
|
||||
}
|
||||
</script>
|
||||
|
||||
<template>
|
||||
<q-btn
|
||||
color="white"
|
||||
text-color="black"
|
||||
label="Del"
|
||||
@click="handleDeleteCookie"
|
||||
/>
|
||||
|
||||
<q-btn
|
||||
color="white"
|
||||
text-color="black"
|
||||
label="Show Cookies"
|
||||
@click="showCookies"
|
||||
/>
|
||||
</template>
|
||||
|
||||
<style scoped></style>
|
||||
|
|
@ -1,28 +1,17 @@
|
|||
<script setup lang="ts">
|
||||
import { onMounted } from 'vue'
|
||||
import { useRoute } from 'vue-router'
|
||||
import { kcAuthen } from "@/plugins/keycloak";
|
||||
import { onMounted } from "vue";
|
||||
import { useRoute } from "vue-router";
|
||||
|
||||
const route = useRoute()
|
||||
const route = useRoute();
|
||||
|
||||
function setCookie(name: string, value: any, days: number) {
|
||||
let expires = ''
|
||||
if (days) {
|
||||
const date = new Date()
|
||||
date.setTime(date.getTime() + days * 24 * 60 * 55 * 1000)
|
||||
expires = '; expires=' + date.toUTCString()
|
||||
onMounted(() => {
|
||||
if (route.query.token && route.query.accessToken) {
|
||||
// console.log('query', route.query.token)
|
||||
// console.log('accessToken', route.query.accessToken)
|
||||
kcAuthen(route.query.token.toString(), route.query.accessToken.toString());
|
||||
}
|
||||
document.cookie = name + '=' + (value || '') + expires + '; path=/'
|
||||
}
|
||||
|
||||
onMounted(async () => {
|
||||
console.log('query', route.query.token)
|
||||
console.log('accessToken', route.query.accessToken)
|
||||
|
||||
setCookie('BMAHRIS_KEYCLOAK_IDENTITY', route.query.token, 1)
|
||||
setCookie('BMAHRIS_KEYCLOAK_REFRESH', route.query.accessToken, 1)
|
||||
|
||||
window.location.href = '/'
|
||||
})
|
||||
});
|
||||
</script>
|
||||
|
||||
<template>
|
||||
|
|
|
|||
|
|
@ -2,7 +2,7 @@
|
|||
<script setup lang="ts">
|
||||
import { ref, onMounted } from "vue";
|
||||
import axios from "axios";
|
||||
import keycloak, { keycloakConfig } from "@/plugins/keycloak";
|
||||
import keycloak, { keycloakConfig, kcAuthen } from "@/plugins/keycloak";
|
||||
import { useRouter } from "vue-router";
|
||||
import { useQuasar } from "quasar";
|
||||
import { useCounterMixin } from "@/stores/mixin";
|
||||
|
|
@ -13,16 +13,6 @@ 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 username = ref<string>("");
|
||||
const password = ref<string>("");
|
||||
async function onSubmit() {
|
||||
|
|
@ -48,10 +38,8 @@ async function onSubmit() {
|
|||
},
|
||||
}
|
||||
)
|
||||
.then(async (res) => {
|
||||
await setCookie("BMAHRIS_KEYCLOAK_IDENTITY", res.data.access_token, 1);
|
||||
await setCookie("BMAHRIS_KEYCLOAK_REFRESH", res.data.refresh_token, 1);
|
||||
window.location.href = "/";
|
||||
.then((res) => {
|
||||
kcAuthen(res.data.access_token, res.data.refresh_token);
|
||||
})
|
||||
.catch((err) => {
|
||||
messageError($q, err, "ชื่อผู้ใช้หรือรหัสผ่านไม่ถูกต้อง");
|
||||
|
|
@ -59,15 +47,8 @@ async function onSubmit() {
|
|||
.finally(() => {
|
||||
hideLoader();
|
||||
});
|
||||
|
||||
// if (response.status !== 200) {
|
||||
// messageError($q, err)
|
||||
// } else {
|
||||
// await setCookie('BMAHRIS_KEYCLOAK_IDENTITY', response.data.access_token, 1)
|
||||
// await setCookie('BMAHRIS_KEYCLOAK_REFRESH', response.data.refresh_token, 1)
|
||||
// router.push('/')
|
||||
// }
|
||||
}
|
||||
|
||||
onMounted(() => {
|
||||
if (keycloak.authenticated) {
|
||||
console.log("authenticated", keycloak.authenticated);
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue