diff --git a/src/main.ts b/src/main.ts
index 24545e6ac..df8024dbe 100644
--- a/src/main.ts
+++ b/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");
diff --git a/src/modules/00_support/router.ts b/src/modules/00_support/router.ts
index 9e7f32720..659c27e00 100644
--- a/src/modules/00_support/router.ts
+++ b/src/modules/00_support/router.ts
@@ -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",
- // },
- // },
];
diff --git a/src/plugins/http.ts b/src/plugins/http.ts
index 8aebceb21..e278860dd 100644
--- a/src/plugins/http.ts
+++ b/src/plugins/http.ts
@@ -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")
diff --git a/src/plugins/keycloak.ts b/src/plugins/keycloak.ts
index 8c74003c3..19e0f9aff 100644
--- a/src/plugins/keycloak.ts
+++ b/src/plugins/keycloak.ts
@@ -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,
+};
diff --git a/src/router/index.ts b/src/router/index.ts
index 79747b623..a52150311 100644
--- a/src/router/index.ts
+++ b/src/router/index.ts
@@ -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();
diff --git a/src/views/MainLayout.vue b/src/views/MainLayout.vue
index 0fe14917c..548d8be97 100644
--- a/src/views/MainLayout.vue
+++ b/src/views/MainLayout.vue
@@ -1,6 +1,6 @@
-
-
-
-
-
-
-
-
diff --git a/src/views/auth.vue b/src/views/auth.vue
index 13301ef08..d27967d8e 100644
--- a/src/views/auth.vue
+++ b/src/views/auth.vue
@@ -1,28 +1,17 @@
diff --git a/src/views/login.vue b/src/views/login.vue
index 459c7f719..293d6d29c 100644
--- a/src/views/login.vue
+++ b/src/views/login.vue
@@ -2,7 +2,7 @@