diff --git a/src/assets/keycloak-bg.png b/src/assets/keycloak-bg.png new file mode 100644 index 0000000..f6b0837 Binary files /dev/null and b/src/assets/keycloak-bg.png differ diff --git a/src/assets/keycloak-logo-poc.png b/src/assets/keycloak-logo-poc.png new file mode 100644 index 0000000..f16aa38 Binary files /dev/null and b/src/assets/keycloak-logo-poc.png differ diff --git a/src/main.ts b/src/main.ts index b05a38c..9d6f0ed 100644 --- a/src/main.ts +++ b/src/main.ts @@ -37,6 +37,7 @@ app.component( app.config.globalProperties.$http = http +// authen with keycloak client function getCookie(name: string) { const nameEQ = name + '=' const ca = document.cookie.split(';') @@ -61,9 +62,11 @@ if (kcToken && kcRefreshToken) { }) .then((authenticated) => { console.log('authenticated', authenticated) - // // // if (authenticated) { - // // // app.mount('#app') - // // // } + if (!authenticated) { + window.location.reload() + } else { + console.log('Authenticated') + } }) .catch((err) => { console.error('Keycloak initialization failed:', err) diff --git a/src/plugins/keycloak.ts b/src/plugins/keycloak.ts index 3865d66..5c0ebe4 100644 --- a/src/plugins/keycloak.ts +++ b/src/plugins/keycloak.ts @@ -1,10 +1,13 @@ +// authen with keycloak client import Keycloak from 'keycloak-js' const keycloakConfig = { - url: 'https://id.frappet.synology.me/auth', + url: 'https://id.frappet.synology.me', realm: 'bma-ehr', clientId: 'gettoken', + clientSecret: 'qsFwDb5anVoXKKwoeivrByIn9VYWQNRn', } const keycloak = new Keycloak(keycloakConfig) export default keycloak +export { keycloakConfig } diff --git a/src/router/index.ts b/src/router/index.ts index e4a76fa..7e499be 100644 --- a/src/router/index.ts +++ b/src/router/index.ts @@ -2,6 +2,7 @@ import { createRouter, createWebHistory } from 'vue-router' import HomeView from '@/views/HomeView.vue' import MapView from '@/views/MapView.vue' import MainView from '@/views/MainView.vue' +const loginView = () => import('@/views/login.vue') import keycloak from '@/plugins/keycloak' @@ -67,25 +68,23 @@ const router = createRouter({ }, ], }, + // authen with keycloak client + { + path: '/login', + name: 'loginMain', + component: loginView, + meta: { + Auth: false, + }, + }, ], }) +// authen with keycloak client router.beforeEach((to, from, next) => { if (to.meta.Auth) { - console.log('keycloak===>', keycloak) - console.log('keycloak authenticated===>', keycloak.authenticated) - - if (keycloak.authenticated === undefined) { - window.location.href = 'https://bma-sso.frappet.synology.me' - // } else { - // // keycloak.updateToken(60); - // // const role = keycloak.tokenParsed?.role - // // if (role.includes(to.meta.Role)) { - // next() - // // } else { - // // next({ path: '' }) - // // // next(); - // // } + if (keycloak.authenticated === undefined && to.meta.Auth) { + window.location.href = '/login' } } else { next() diff --git a/src/stores/mixin.ts b/src/stores/mixin.ts index 77ef612..80dbd12 100644 --- a/src/stores/mixin.ts +++ b/src/stores/mixin.ts @@ -128,7 +128,7 @@ export const useCounterMixin = defineStore('mixin', () => { }) } - const messageError = (q: any, e: any = '') => { + const messageError = (q: any, e: any = '', msg: string = '') => { // q.dialog.hide(); if (e.response !== undefined) { if (e.response.data.status !== undefined) { @@ -159,17 +159,30 @@ export const useCounterMixin = defineStore('mixin', () => { } } else { if (e.response.status == 401) { - //invalid_token - q.dialog({ - component: CustomComponent, - componentProps: { - title: `พบข้อผิดพลาด`, - message: `ล็อกอินหมดอายุ กรุณาล็อกอินใหม่อีกครั้ง`, - icon: 'warning', - color: 'red', - onlycancel: true, - }, - }) + if (msg !== '') { + q.dialog({ + component: CustomComponent, + componentProps: { + title: `พบข้อผิดพลาด`, + message: msg, + icon: 'warning', + color: 'red', + onlycancel: true, + }, + }) + } else { + //invalid_token + q.dialog({ + component: CustomComponent, + componentProps: { + title: `พบข้อผิดพลาด`, + message: `ล็อกอินหมดอายุ กรุณาล็อกอินใหม่อีกครั้ง`, + icon: 'warning', + color: 'red', + onlycancel: true, + }, + }) + } } else if (e.response.data.successful === false) { q.dialog({ component: CustomComponent, diff --git a/src/views/MainView.vue b/src/views/MainView.vue index 9eb8f1a..3dbd8d5 100644 --- a/src/views/MainView.vue +++ b/src/views/MainView.vue @@ -98,13 +98,14 @@ function onClickLogout() { persistent: true, }).onOk(async () => { // keycloak.logout() + // authen with keycloak client await deleteCookie('BMAHRIS_KEYCLOAK_IDENTITY') await deleteCookie('BMAHRIS_KEYCLOAK_REFRESH') - window.location.href = 'https://bma-sso.frappet.synology.me' + window.location.href = '/login' }) } -function deleteCookie(name) { +function deleteCookie(name: string) { document.cookie = `${name}=; expires=Thu, 01 Jan 1970 00:00:00 UTC; path=/;` } diff --git a/src/views/login.vue b/src/views/login.vue new file mode 100644 index 0000000..ea2b1c6 --- /dev/null +++ b/src/views/login.vue @@ -0,0 +1,327 @@ + + + + + + + + + + ระบบบริหารทรัพยากรบุคคล + + ของกรุงเทพมหานคร + + + + + เข้าใช้งานระบบ + + + + + ชื่อผู้ใช้งาน + + + + รหัสผ่าน + + + + + + + + ล็อคอินค้างไว้ + + + + ลืมรหัสผ่าน + + + + + + + + + + + + + + +