ต่อ auth keycloak

This commit is contained in:
Kittapath 2023-03-17 10:26:07 +07:00
parent 04be324441
commit aecfb85e14
2 changed files with 27 additions and 9 deletions

View file

@ -1,7 +1,7 @@
/** /**
* front connect to keycloak * front connect to keycloak
*/ */
import Keycloak from "keycloak-js"; import Keycloak from 'keycloak-js'
// import config from "../app.config"; // import config from "../app.config";
// import http from "../shared/http"; // import http from "../shared/http";
// import router from "../router"; // import router from "../router";
@ -10,14 +10,14 @@ const initOptions = {
// url: "https://keycloak.frappet.synology.me/auth/", // url: "https://keycloak.frappet.synology.me/auth/",
// realm: "bma-ehr", // realm: "bma-ehr",
// clientId: "bma-ehr-vue3", // clientId: "bma-ehr-vue3",
url: "https://identity.frappet.com/", url: 'https://identity.frappet.com/',
realm: "bma-ehr", realm: 'exam_test',
clientId: "bma-ehr-vue3", clientId: 'exam_vue3'
}; //option keycloak ที่จะ connect } //option keycloak ที่จะ connect
const keycloak = Keycloak(initOptions); const keycloak = Keycloak(initOptions)
keycloak.onAuthSuccess = () => {}; //เพิ่มlogin สำเร็จจะมาทำฟังก์ชันนี้ keycloak.onAuthSuccess = () => {} //เพิ่มlogin สำเร็จจะมาทำฟังก์ชันนี้
await keycloak.init({ checkLoginIframe: false }); //ทำการ connect keycloak await keycloak.init({ checkLoginIframe: false }) //ทำการ connect keycloak
export default keycloak; export default keycloak

View file

@ -3,6 +3,8 @@ import HomeView from '../views/HomeView.vue'
import Exam from '@/modules/01_exam/router' import Exam from '@/modules/01_exam/router'
import Meta02 from '@/modules/02_meta/router' import Meta02 from '@/modules/02_meta/router'
import keycloak from "@/plugins/keycloak";
const MainLayout = () => import('@/views/MainLayout.vue') const MainLayout = () => import('@/views/MainLayout.vue')
const router = createRouter({ 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 export default router