diff --git a/src/plugins/keycloak.ts b/src/plugins/keycloak.ts index 5d64de9..7aeee92 100644 --- a/src/plugins/keycloak.ts +++ b/src/plugins/keycloak.ts @@ -1,7 +1,7 @@ /** * front connect to keycloak */ -import Keycloak from "keycloak-js"; +import Keycloak from 'keycloak-js' // import config from "../app.config"; // import http from "../shared/http"; // import router from "../router"; @@ -10,14 +10,14 @@ const initOptions = { // url: "https://keycloak.frappet.synology.me/auth/", // realm: "bma-ehr", // clientId: "bma-ehr-vue3", - url: "https://identity.frappet.com/", - realm: "bma-ehr", - clientId: "bma-ehr-vue3", -}; //option keycloak ที่จะ connect + url: 'https://identity.frappet.com/', + realm: 'exam_test', + clientId: 'exam_vue3' +} //option keycloak ที่จะ connect -const keycloak = Keycloak(initOptions); +const keycloak = Keycloak(initOptions) -keycloak.onAuthSuccess = () => {}; //เพิ่มlogin สำเร็จจะมาทำฟังก์ชันนี้ +keycloak.onAuthSuccess = () => {} //เพิ่มlogin สำเร็จจะมาทำฟังก์ชันนี้ -await keycloak.init({ checkLoginIframe: false }); //ทำการ connect keycloak -export default keycloak; +await keycloak.init({ checkLoginIframe: false }) //ทำการ connect keycloak +export default keycloak diff --git a/src/router/index.ts b/src/router/index.ts index 8ab2472..1606940 100644 --- a/src/router/index.ts +++ b/src/router/index.ts @@ -3,6 +3,8 @@ import HomeView from '../views/HomeView.vue' import Exam from '@/modules/01_exam/router' import Meta02 from '@/modules/02_meta/router' +import keycloak from "@/plugins/keycloak"; + const MainLayout = () => import('@/views/MainLayout.vue') const router = createRouter({ @@ -56,5 +58,21 @@ const router = createRouter({ // } // ] // }) +router.beforeEach((to, from, next) => { + if (to.meta.Auth) { + if (!keycloak.authenticated) { + keycloak.login({ + redirectUri: `${window.location.protocol}//${window.location.host}${to.path}`, + locale: "th", + }); + } else { + // keycloak.updateToken(60); + next(); + } + } else { + next(); + } + // next(); + }); export default router