From db9d7e362dcd5d08688a6eb32521b0499f9ce809 Mon Sep 17 00:00:00 2001 From: Alicia Sykes Date: Sun, 28 Apr 2024 20:00:17 +0100 Subject: [PATCH] =?UTF-8?q?=F0=9F=9B=82=20Remove=20page=20reload=20on=20au?= =?UTF-8?q?th=20failure=20(#981)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- services/get-user.js | 16 ++++++++-------- src/main.js | 16 ++++++++++------ src/utils/HeaderAuth.js | 7 ++++--- 3 files changed, 22 insertions(+), 17 deletions(-) diff --git a/services/get-user.js b/services/get-user.js index b9c9e457..4d10ccdd 100644 --- a/services/get-user.js +++ b/services/get-user.js @@ -1,15 +1,15 @@ module.exports = (config, req) => { try { - if ( config.appConfig.auth.enableHeaderAuth ) { - const userHeader = config.appConfig.auth.headerAuth.userHeader; - const proxyWhitelist = config.appConfig.auth.headerAuth.proxyWhitelist; - if ( proxyWhitelist.includes(req.socket.remoteAddress) ) { - return { "success": true, "user": req.headers[userHeader.toLowerCase()] }; + if (config.appConfig.auth.enableHeaderAuth) { + const { userHeader } = config.appConfig.auth.headerAuth; + const { proxyWhitelist } = config.appConfig.auth.headerAuth; + if (proxyWhitelist.includes(req.socket.remoteAddress)) { + return { success: true, user: req.headers[userHeader.toLowerCase()] }; } } return {}; } catch (e) { - console.warn("Error get-user: ", e); - return { 'success': false }; + console.warn('Error get-user: ', e); + return { success: false }; } -}; \ No newline at end of file +}; diff --git a/src/main.js b/src/main.js index 65f54a18..e112b4a8 100644 --- a/src/main.js +++ b/src/main.js @@ -23,6 +23,7 @@ import { toastedOptions, tooltipOptions, language as defaultLanguage } from '@/u import { initKeycloakAuth, isKeycloakEnabled } from '@/utils/KeycloakAuth'; import { initHeaderAuth, isHeaderAuthEnabled } from '@/utils/HeaderAuth'; import Keys from '@/utils/StoreMutations'; +import ErrorHandler from '@/utils/ErrorHandler'; // Initialize global Vue components Vue.use(VueI18n); @@ -61,16 +62,19 @@ const mount = () => new Vue({ }).$mount('#app'); store.dispatch(Keys.INITIALIZE_CONFIG).then(() => { - // Keycloak is enabled, redirect to KC login page - if (isKeycloakEnabled()) { + if (isKeycloakEnabled()) { // If Keycloak is enabled, initialize auth initKeycloakAuth() .then(() => mount()) - .catch(() => window.location.reload()); - } else if (isHeaderAuthEnabled()) { + .catch((e) => { + ErrorHandler('Failed to authenticate with Keycloak', e); + }); + } else if (isHeaderAuthEnabled()) { // If header auth is enabled, initialize auth initHeaderAuth() .then(() => mount()) - .catch(() => window.location.reload()); - } else { // If Keycloak not enabled, then proceed straight to the app + .catch((e) => { + ErrorHandler('Failed to authenticate with server', e); + }); + } else { // If no third-party auth, just mount the app as normal mount(); } }); diff --git a/src/utils/HeaderAuth.js b/src/utils/HeaderAuth.js index 33475de6..ff3369e2 100644 --- a/src/utils/HeaderAuth.js +++ b/src/utils/HeaderAuth.js @@ -3,7 +3,7 @@ import sha256 from 'crypto-js/sha256'; import ConfigAccumulator from '@/utils/ConfigAccumalator'; import { cookieKeys, localStorageKeys, serviceEndpoints } from '@/utils/defaults'; import { InfoHandler, ErrorHandler, InfoKeys } from '@/utils/ErrorHandler'; -import { logout } from '@/utils/Auth'; +import { logout as authLogout } from '@/utils/Auth'; const getAppConfig = () => { const Accumulator = new ConfigAccumulator(); @@ -22,7 +22,6 @@ class HeaderAuth { this.users = auth.users; } - /* eslint-disable class-methods-use-this */ login() { return new Promise((resolve, reject) => { const baseUrl = process.env.VUE_APP_DOMAIN || window.location.origin; @@ -44,6 +43,7 @@ class HeaderAuth { } }); } catch (e) { + ErrorHandler('Error while trying to login using header authentication', e); reject(e); } } @@ -51,8 +51,9 @@ class HeaderAuth { }); } + // eslint-disable-next-line class-methods-use-this logout() { - logout(); + authLogout(); } }