From 104c1bfedd265cbbbcaadba036a5735c55124edb Mon Sep 17 00:00:00 2001
From: Christoph Reiter <reiter.christoph@gmail.com>
Date: Tue, 16 Nov 2021 14:41:07 +0100
Subject: [PATCH] auth: in case anything regarding fetching the user fails,
 just give up

Instead of throwing an error.

So the login process completes, but the user will not have any roles in that case.
This helps in case the API isn't set up but the user wants to try the frontend app.
---
 packages/auth/src/auth-keycloak.js | 29 ++++++++++++-----------------
 1 file changed, 12 insertions(+), 17 deletions(-)

diff --git a/packages/auth/src/auth-keycloak.js b/packages/auth/src/auth-keycloak.js
index ddae039b..643b33bb 100644
--- a/packages/auth/src/auth-keycloak.js
+++ b/packages/auth/src/auth-keycloak.js
@@ -84,27 +84,13 @@ export class AuthKeycloak extends AdapterLitElement {
 
     async _fetchUser(userId) {
         let jsonld;
-        try {
-            jsonld = await JSONLD.getInstance(this.entryPointUrl, this.lang);
-        } catch(error) {
-            // Server is down, just give up.
-            return {
-                roles: [],
-            };
-        }
+        jsonld = await JSONLD.getInstance(this.entryPointUrl, this.lang);
         let baseUrl = '';
         try {
             baseUrl = jsonld.getApiUrlForEntityName("FrontendUser");
         } catch(error) {
             // backwards compat
-            try {
-                baseUrl = jsonld.getApiUrlForEntityName("Person");
-            } catch(error) {
-                // There are no entities, just give up.
-                return {
-                    roles: [],
-                };
-            }
+            baseUrl = jsonld.getApiUrlForEntityName("Person");
         }
         const apiUrl = baseUrl + '/' + encodeURIComponent(userId);
 
@@ -137,7 +123,16 @@ export class AuthKeycloak extends AdapterLitElement {
             let userChanged = (userId !== this._userId);
             if (userChanged) {
                 this._userId = userId;
-                let user = await this._fetchUser(userId);
+                let user;
+                try {
+                    user = await this._fetchUser(userId);
+                } catch (error) {
+                    // In case fetching the user failed then likely the API backend
+                    // is not set up or broken. Return a user without any roles so we
+                    // can show something at least.
+                    console.error(error);
+                    user = {roles: []};
+                }
                 if (userId === this._userId) {
                     this._user = user;
                 }
-- 
GitLab