From 06350c067efbf8b022a729159c412453cab67243 Mon Sep 17 00:00:00 2001
From: Christoph Reiter <reiter.christoph@gmail.com>
Date: Mon, 25 Apr 2022 15:32:31 +0200
Subject: [PATCH] auth: handle the case when the keycloak server is offline

Up until now it would throw and never set a login status. This would lead
to the login process never completing, and in our case the app-shell
would never show the app and only show a white page.

Instead set the status to logged out and show a notification.
---
 packages/auth/src/auth-keycloak.js         | 28 +++++++++++++++-------
 packages/auth/src/i18n/de/translation.json |  1 +
 packages/auth/src/i18n/en/translation.json |  1 +
 3 files changed, 22 insertions(+), 8 deletions(-)

diff --git a/packages/auth/src/auth-keycloak.js b/packages/auth/src/auth-keycloak.js
index db3ded9d..dc4dcce2 100644
--- a/packages/auth/src/auth-keycloak.js
+++ b/packages/auth/src/auth-keycloak.js
@@ -3,6 +3,7 @@ import JSONLD from '@dbp-toolkit/common/jsonld';
 import {KeycloakWrapper} from './keycloak.js';
 import {LoginStatus} from './util.js';
 import {AdapterLitElement} from '@dbp-toolkit/provider/src/adapter-lit-element';
+import {send} from '@dbp-toolkit/common/notification';
 
 /**
  * Keycloak auth web component
@@ -219,17 +220,28 @@ export class AuthKeycloak extends AdapterLitElement {
         this._kcwrapper.addEventListener('changed', this._onKCChanged);
 
         const handleLogin = async () => {
-            if (this.forceLogin || this._kcwrapper.isLoggingIn()) {
-                this._setLoginStatus(LoginStatus.LOGGING_IN);
-                await this._kcwrapper.login({lang: this.lang, scope: this.scope || ''});
-            } else if (this.tryLogin) {
-                this._setLoginStatus(LoginStatus.LOGGING_IN);
-                await this._kcwrapper.tryLogin();
-                if (!this._authenticated) {
+            try {
+                if (this.forceLogin || this._kcwrapper.isLoggingIn()) {
+                    this._setLoginStatus(LoginStatus.LOGGING_IN);
+                    await this._kcwrapper.login({lang: this.lang, scope: this.scope || ''});
+                } else if (this.tryLogin) {
+                    this._setLoginStatus(LoginStatus.LOGGING_IN);
+                    await this._kcwrapper.tryLogin();
+                    if (!this._authenticated) {
+                        this._setLoginStatus(LoginStatus.LOGGED_OUT);
+                    }
+                } else {
                     this._setLoginStatus(LoginStatus.LOGGED_OUT);
                 }
-            } else {
+            } catch (error) {
+                // In case the keycloak server is offline for example
                 this._setLoginStatus(LoginStatus.LOGGED_OUT);
+                send({
+                    summary: this._i18n.t('login-failed'),
+                    type: 'danger',
+                    timeout: 5,
+                });
+                throw error;
             }
         };
 
diff --git a/packages/auth/src/i18n/de/translation.json b/packages/auth/src/i18n/de/translation.json
index 1fa91133..46959e2a 100644
--- a/packages/auth/src/i18n/de/translation.json
+++ b/packages/auth/src/i18n/de/translation.json
@@ -1,4 +1,5 @@
 {
     "login": "Anmelden",
+    "login-failed": "Kommunikation mit dem Anmeldeserver fehlgeschlagen",
     "logout": "Abmelden"
 }
diff --git a/packages/auth/src/i18n/en/translation.json b/packages/auth/src/i18n/en/translation.json
index 8bfc42ac..1cd772c5 100644
--- a/packages/auth/src/i18n/en/translation.json
+++ b/packages/auth/src/i18n/en/translation.json
@@ -1,4 +1,5 @@
 {
     "login": "Login",
+    "login-failed": "Communication with the login server failed",
     "logout": "Logout"
 }
-- 
GitLab