From c1f0ac79393d9ff50c74f50aae940fe088d27cce Mon Sep 17 00:00:00 2001 From: waruneeauy Date: Thu, 25 Jul 2024 11:40:47 +0700 Subject: [PATCH] updated authen --- src/main.ts | 38 ++++++++++++++++++++------------------ src/plugins/http.ts | 2 ++ src/router/index.ts | 5 ++++- src/views/auth.vue | 4 ++-- src/views/login.vue | 18 +++++++++++------- 5 files changed, 39 insertions(+), 28 deletions(-) diff --git a/src/main.ts b/src/main.ts index 9d6f0ed..b5b7a7b 100644 --- a/src/main.ts +++ b/src/main.ts @@ -53,24 +53,26 @@ const kcToken = getCookie('BMAHRIS_KEYCLOAK_IDENTITY') const kcRefreshToken = getCookie('BMAHRIS_KEYCLOAK_REFRESH') if (kcToken && kcRefreshToken) { - keycloak - .init({ - // onLoad: 'login-required', - checkLoginIframe: false, - token: kcToken, - refreshToken: kcRefreshToken, - }) - .then((authenticated) => { - console.log('authenticated', authenticated) - if (!authenticated) { - window.location.reload() - } else { - console.log('Authenticated') - } - }) - .catch((err) => { - console.error('Keycloak initialization failed:', err) - }) + keycloak.init({ + // onLoad: 'login-required', + checkLoginIframe: false, + token: kcToken, + refreshToken: kcRefreshToken, + }) + keycloak.authenticated = true + // .then((authenticated) => { + // console.log('authenticated', authenticated) + // if (!authenticated) { + // window.location.reload() + // } else { + // console.log('Authenticated') + // } + // }) + // .catch((err) => { + // console.error('Keycloak initialization failed:', err) + // }) +} else { + keycloak.authenticated = false } app.mount('#app') diff --git a/src/plugins/http.ts b/src/plugins/http.ts index b82f8c1..6a7eb17 100644 --- a/src/plugins/http.ts +++ b/src/plugins/http.ts @@ -31,6 +31,8 @@ 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' + // Store.commit("SET_ERROR_MESSAGE", error.response.data.message); // Store.commit("REMOVE_ACCESS_TOKEN") } diff --git a/src/router/index.ts b/src/router/index.ts index 7e499be..ced0e07 100644 --- a/src/router/index.ts +++ b/src/router/index.ts @@ -82,8 +82,11 @@ const router = createRouter({ // authen with keycloak client router.beforeEach((to, from, next) => { + // console.log('keycloak==>', keycloak) + console.log('keycloak==>', keycloak.authenticated) + if (to.meta.Auth) { - if (keycloak.authenticated === undefined && to.meta.Auth) { + if (keycloak.authenticated === false && to.meta.Auth) { window.location.href = '/login' } } else { diff --git a/src/views/auth.vue b/src/views/auth.vue index 4c2d5c4..13301ef 100644 --- a/src/views/auth.vue +++ b/src/views/auth.vue @@ -1,5 +1,5 @@