From cb7e78cb9359ddba74587866552ccf37249ba597 Mon Sep 17 00:00:00 2001
From: Christoph Reiter <reiter.christoph@gmail.com>
Date: Tue, 23 Mar 2021 12:22:40 +0100
Subject: [PATCH] jsonld: don't wait for a token for the entity lookup

We can access that API without a token.
---
 packages/auth/src/auth-keycloak.js            |  2 +-
 .../src/check-in-place-select.js              |  2 +-
 packages/common/jsonld.js                     | 30 +++++++++++--------
 .../src/organization-select.js                |  2 +-
 packages/person-profile/src/person-profile.js |  2 +-
 packages/person-select/src/person-select.js   |  2 +-
 6 files changed, 23 insertions(+), 17 deletions(-)

diff --git a/packages/auth/src/auth-keycloak.js b/packages/auth/src/auth-keycloak.js
index 1ad5f02f..eac61afc 100644
--- a/packages/auth/src/auth-keycloak.js
+++ b/packages/auth/src/auth-keycloak.js
@@ -152,7 +152,7 @@ export class AuthKeycloak extends AdapterLitElement {
         }
 
         this.sendSetPropertyEvent('auth', auth);
-        JSONLD.doInitializationOnce(this.entryPointUrl, this.token);
+        JSONLD.doInitializationOnce(this.entryPointUrl);
     }
 
     _setLoginStatus(status, force) {
diff --git a/packages/check-in-place-select/src/check-in-place-select.js b/packages/check-in-place-select/src/check-in-place-select.js
index 66216515..617ce6be 100644
--- a/packages/check-in-place-select/src/check-in-place-select.js
+++ b/packages/check-in-place-select/src/check-in-place-select.js
@@ -310,7 +310,7 @@ export class CheckInPlaceSelect extends ScopedElementsMixin(AdapterLitElement) {
                     this.initJSONLD(true);
                     break;
                 case "auth":
-                    JSONLD.doInitializationOnce(this.entryPointUrl, this.auth.token);
+                    JSONLD.doInitializationOnce(this.entryPointUrl);
                     break;
             }
         });
diff --git a/packages/common/jsonld.js b/packages/common/jsonld.js
index 1e240a60..3fff36bf 100644
--- a/packages/common/jsonld.js
+++ b/packages/common/jsonld.js
@@ -41,28 +41,26 @@ export default class JSONLD {
     }
 
     /**
-     * This should be run as soon as an api token is available (can be run multiple times)
+     * This should be run as soon as possible (can be run multiple times)
      *
      * @param apiUrl
-     * @param token
      */
-    static doInitializationOnce(apiUrl, token) {
-        // console.log("doInitializationOnce", apiUrl, token);
+    static doInitializationOnce(apiUrl) {
+        // console.log("doInitializationOnce", apiUrl);
 
-        // check if token is not set or api call was already started
-        if (!apiUrl || !token || JSONLD.initStarted[apiUrl] !== undefined) {
+        // check if api call was already started
+        if (!apiUrl || JSONLD.initStarted[apiUrl] !== undefined) {
             return;
         }
 
         JSONLD.initStarted[apiUrl] = true;
-        JSONLD.doInitialization(apiUrl, token);
-        // console.log("doInitializationOnce Done", apiUrl, token);
+        JSONLD.doInitialization(apiUrl);
+        // console.log("doInitializationOnce Done", apiUrl);
     }
 
-    static doInitialization(apiUrl, token) {
+    static doInitialization(apiUrl) {
         const xhr = new XMLHttpRequest();
         xhr.open("GET", apiUrl, true);
-        xhr.setRequestHeader('Authorization', 'Bearer ' + token);
 
         xhr.onreadystatechange = function () {
             if (xhr.readyState !== 4) {
@@ -143,7 +141,13 @@ export default class JSONLD {
         JSONLD.instances[apiUrl] = instance;
 
         // return the initialized JSONLD object
-        for (const fnc of JSONLD.successFunctions[apiUrl]) if (typeof fnc == 'function') fnc(instance);
+        if (JSONLD.successFunctions[apiUrl] !== undefined) {
+            for (const fnc of JSONLD.successFunctions[apiUrl]) {
+                if (typeof fnc == 'function') {
+                    fnc(instance);
+                }
+            }
+        }
         JSONLD.successFunctions[apiUrl] = [];
     }
 
@@ -154,7 +158,9 @@ export default class JSONLD {
      * @param message
      */
     static executeFailureFunctions(apiUrl, message = "") {
-        for (const fnc of JSONLD.failureFunctions[apiUrl]) if (typeof fnc == 'function') fnc();
+        if (JSONLD.failureFunctions[apiUrl] !== undefined) {
+            for (const fnc of JSONLD.failureFunctions[apiUrl]) if (typeof fnc == 'function') fnc();
+        }
         JSONLD.failureFunctions[apiUrl] = [];
 
         if (message !== "") {
diff --git a/packages/organization-select/src/organization-select.js b/packages/organization-select/src/organization-select.js
index f368d8f2..954c6ae7 100644
--- a/packages/organization-select/src/organization-select.js
+++ b/packages/organization-select/src/organization-select.js
@@ -199,7 +199,7 @@ export class OrganizationSelect extends AdapterLitElement {
                     }, {}, this.lang);
                     break;
                 case "auth":
-                    JSONLD.doInitializationOnce(this.entryPointUrl, this.auth.token);
+                    JSONLD.doInitializationOnce(this.entryPointUrl);
                     this.initAuthPersonOnce().then();
                     break;
                 default:
diff --git a/packages/person-profile/src/person-profile.js b/packages/person-profile/src/person-profile.js
index 2b6846ea..919a9cdf 100644
--- a/packages/person-profile/src/person-profile.js
+++ b/packages/person-profile/src/person-profile.js
@@ -68,7 +68,7 @@ export class PersonProfile extends DBPLitElement {
                     }
                     break;
                 case "auth":
-                    JSONLD.doInitializationOnce(this.entryPointUrl, this.auth.token);
+                    JSONLD.doInitializationOnce(this.entryPointUrl);
                     break;
                 default:
             }
diff --git a/packages/person-select/src/person-select.js b/packages/person-select/src/person-select.js
index ff9e380c..1575c5af 100644
--- a/packages/person-select/src/person-select.js
+++ b/packages/person-select/src/person-select.js
@@ -291,7 +291,7 @@ export class PersonSelect extends ScopedElementsMixin(AdapterLitElement) {
                     this.initJSONLD(true);
                     break;
                 case "auth":
-                    JSONLD.doInitializationOnce(this.entryPointUrl, this.auth.token);
+                    JSONLD.doInitializationOnce(this.entryPointUrl);
                     this.active = this.authenticated();
                     break;
             }
-- 
GitLab