ต่อ 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
*/
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

View file

@ -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