diff --git a/packages/auth/README.md b/packages/auth/README.md index c9d99d387e4e19386b4d834881646228595caa38..8d4551e4e47fabb6e833e2b352516e5f7c1eba51 100644 --- a/packages/auth/README.md +++ b/packages/auth/README.md @@ -38,7 +38,6 @@ npm i @dbp-toolkit/auth ### Events to listen to -- `dbp-auth-init`: Keycloak init event - happens once - `dbp-auth-person-init`: Keycloak person init event - the person entity was loaded from the server - `dbp-auth-keycloak-data-update`: Keycloak data was updated - happens for example every time after a token refresh diff --git a/packages/auth/src/auth-keycloak.js b/packages/auth/src/auth-keycloak.js index 38c3894231f95a0930ff1a3f2f346f01a120cb03..d0ae9864a82d6e0a8b28d2a27d882c19581afaa7 100644 --- a/packages/auth/src/auth-keycloak.js +++ b/packages/auth/src/auth-keycloak.js @@ -10,7 +10,7 @@ import {AdapterLitElement} from "@dbp-toolkit/provider/src/adapter-lit-element"; * Keycloak auth web component * https://www.keycloak.org/docs/latest/securing_apps/index.html#_javascript_adapter * - * Dispatches an event `dbp-auth-init` and sets some global variables: + * Sets some global variables: * window.DBPAuthSubject: Keycloak username * window.DBPAuthToken: Keycloak token to send with your requests * window.DBPAuthTokenParsed: Keycloak token content @@ -44,7 +44,6 @@ export class AuthKeycloak extends AdapterLitElement { this.idpHint = ''; // Create the events - this.initEvent = new CustomEvent("dbp-auth-init", { "detail": "KeyCloak init event", bubbles: true, composed: true }); this.personInitEvent = new CustomEvent("dbp-auth-person-init", { "detail": "KeyCloak person init event", bubbles: true, composed: true }); this.keycloakDataUpdateEvent = new CustomEvent("dbp-auth-keycloak-data-update", { "detail": "KeyCloak data was updated", bubbles: true, composed: true }); @@ -112,10 +111,6 @@ export class AuthKeycloak extends AdapterLitElement { const that = this; - if (newPerson) { - this.dispatchEvent(this.initEvent); - } - if (newPerson && this.loadPerson) { JSONLD.initialize(this.entryPointUrl, (jsonld) => { // find the correct api url for the current person diff --git a/packages/common/jsonld.js b/packages/common/jsonld.js index e45a7582fb966e9832eabb38bf07250c74652c48..139a54e8923e9e96152d8c6eeb460897912e6486 100644 --- a/packages/common/jsonld.js +++ b/packages/common/jsonld.js @@ -3,6 +3,7 @@ import {send as notify} from './notification'; import * as utils from "./utils"; import {i18n} from "./i18n"; +import {EventBus} from './'; let instances = {}; let successFunctions = {}; @@ -51,18 +52,19 @@ export default class JSONLD { initStarted[apiUrl] = true; - if (window.DBPAuthToken !== undefined) { - JSONLD.doInitialization(apiUrl); - } else { - // window.DBPAuthToken will be set by dbp-auth-init event - window.addEventListener("dbp-auth-init", () => JSONLD.doInitialization(apiUrl)); - } + this._bus = new EventBus(); + this._bus.subscribe('auth-update', (data) => { + if (data.token) { + this._bus.close(); + JSONLD.doInitialization(apiUrl, data.token); + } + }); } - static doInitialization(apiUrl) { + static doInitialization(apiUrl, token) { const xhr = new XMLHttpRequest(); xhr.open("GET", apiUrl, true); - xhr.setRequestHeader('Authorization', 'Bearer ' + window.DBPAuthToken); + xhr.setRequestHeader('Authorization', 'Bearer ' + token); xhr.onreadystatechange = function () { if (xhr.readyState !== 4) { diff --git a/packages/knowledge-base-web-page-element-view/src/knowledge-base-web-page-element-view.js b/packages/knowledge-base-web-page-element-view/src/knowledge-base-web-page-element-view.js index 194a6fcdb49213d3094afa1abd0b827f58b57311..c56aac2ffeb37cd8e0a5c342201b43ca2358f284 100644 --- a/packages/knowledge-base-web-page-element-view/src/knowledge-base-web-page-element-view.js +++ b/packages/knowledge-base-web-page-element-view/src/knowledge-base-web-page-element-view.js @@ -49,21 +49,6 @@ export class KnowledgeBaseWebPageElementView extends ScopedElementsMixin(LitElem }; } - /* - connectedCallback() { - super.connectedCallback(); - const that = this; - - // JSONLD.initialize(this.entryPointUrl, function (jsonld) { - // const apiUrl = jsonld.getApiUrlForEntityName("KnowledgeBaseWebPageElement") + '/' + - // encodeURIComponent(commonUtils.base64EncodeUnicode(encodeURIComponent(that.value))); - // }); - - // disabled, load first on toggle to visible - window.addEventListener("dbp-auth-init", () => that.loadWebPageElement()); - } - */ - /** * Loads the data from the web page element */