🛂 Remove page reload on auth failure (#981)

This commit is contained in:
Alicia Sykes 2024-04-28 20:00:17 +01:00
parent 85de40d950
commit db9d7e362d
3 changed files with 22 additions and 17 deletions

View File

@ -1,15 +1,15 @@
module.exports = (config, req) => { module.exports = (config, req) => {
try { try {
if ( config.appConfig.auth.enableHeaderAuth ) { if (config.appConfig.auth.enableHeaderAuth) {
const userHeader = config.appConfig.auth.headerAuth.userHeader; const { userHeader } = config.appConfig.auth.headerAuth;
const proxyWhitelist = config.appConfig.auth.headerAuth.proxyWhitelist; const { proxyWhitelist } = config.appConfig.auth.headerAuth;
if ( proxyWhitelist.includes(req.socket.remoteAddress) ) { if (proxyWhitelist.includes(req.socket.remoteAddress)) {
return { "success": true, "user": req.headers[userHeader.toLowerCase()] }; return { success: true, user: req.headers[userHeader.toLowerCase()] };
} }
} }
return {}; return {};
} catch (e) { } catch (e) {
console.warn("Error get-user: ", e); console.warn('Error get-user: ', e);
return { 'success': false }; return { success: false };
} }
}; };

View File

@ -23,6 +23,7 @@ import { toastedOptions, tooltipOptions, language as defaultLanguage } from '@/u
import { initKeycloakAuth, isKeycloakEnabled } from '@/utils/KeycloakAuth'; import { initKeycloakAuth, isKeycloakEnabled } from '@/utils/KeycloakAuth';
import { initHeaderAuth, isHeaderAuthEnabled } from '@/utils/HeaderAuth'; import { initHeaderAuth, isHeaderAuthEnabled } from '@/utils/HeaderAuth';
import Keys from '@/utils/StoreMutations'; import Keys from '@/utils/StoreMutations';
import ErrorHandler from '@/utils/ErrorHandler';
// Initialize global Vue components // Initialize global Vue components
Vue.use(VueI18n); Vue.use(VueI18n);
@ -61,16 +62,19 @@ const mount = () => new Vue({
}).$mount('#app'); }).$mount('#app');
store.dispatch(Keys.INITIALIZE_CONFIG).then(() => { store.dispatch(Keys.INITIALIZE_CONFIG).then(() => {
// Keycloak is enabled, redirect to KC login page if (isKeycloakEnabled()) { // If Keycloak is enabled, initialize auth
if (isKeycloakEnabled()) {
initKeycloakAuth() initKeycloakAuth()
.then(() => mount()) .then(() => mount())
.catch(() => window.location.reload()); .catch((e) => {
} else if (isHeaderAuthEnabled()) { ErrorHandler('Failed to authenticate with Keycloak', e);
});
} else if (isHeaderAuthEnabled()) { // If header auth is enabled, initialize auth
initHeaderAuth() initHeaderAuth()
.then(() => mount()) .then(() => mount())
.catch(() => window.location.reload()); .catch((e) => {
} else { // If Keycloak not enabled, then proceed straight to the app ErrorHandler('Failed to authenticate with server', e);
});
} else { // If no third-party auth, just mount the app as normal
mount(); mount();
} }
}); });

View File

@ -3,7 +3,7 @@ import sha256 from 'crypto-js/sha256';
import ConfigAccumulator from '@/utils/ConfigAccumalator'; import ConfigAccumulator from '@/utils/ConfigAccumalator';
import { cookieKeys, localStorageKeys, serviceEndpoints } from '@/utils/defaults'; import { cookieKeys, localStorageKeys, serviceEndpoints } from '@/utils/defaults';
import { InfoHandler, ErrorHandler, InfoKeys } from '@/utils/ErrorHandler'; import { InfoHandler, ErrorHandler, InfoKeys } from '@/utils/ErrorHandler';
import { logout } from '@/utils/Auth'; import { logout as authLogout } from '@/utils/Auth';
const getAppConfig = () => { const getAppConfig = () => {
const Accumulator = new ConfigAccumulator(); const Accumulator = new ConfigAccumulator();
@ -22,7 +22,6 @@ class HeaderAuth {
this.users = auth.users; this.users = auth.users;
} }
/* eslint-disable class-methods-use-this */
login() { login() {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
const baseUrl = process.env.VUE_APP_DOMAIN || window.location.origin; const baseUrl = process.env.VUE_APP_DOMAIN || window.location.origin;
@ -44,6 +43,7 @@ class HeaderAuth {
} }
}); });
} catch (e) { } catch (e) {
ErrorHandler('Error while trying to login using header authentication', e);
reject(e); reject(e);
} }
} }
@ -51,8 +51,9 @@ class HeaderAuth {
}); });
} }
// eslint-disable-next-line class-methods-use-this
logout() { logout() {
logout(); authLogout();
} }
} }