From 01d068761339c6bb083c0bd524895b4654c12757 Mon Sep 17 00:00:00 2001 From: Christoph Reiter <reiter.christoph@gmail.com> Date: Wed, 27 Jan 2021 12:51:55 +0100 Subject: [PATCH] Get rid of the dbp-auth-init event Replace with the event bus for now --- packages/auth/README.md | 1 - packages/auth/src/auth-keycloak.js | 7 +------ packages/common/jsonld.js | 18 ++++++++++-------- .../knowledge-base-web-page-element-view.js | 15 --------------- 4 files changed, 11 insertions(+), 30 deletions(-) diff --git a/packages/auth/README.md b/packages/auth/README.md index c9d99d38..8d4551e4 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 38c38942..d0ae9864 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 e45a7582..139a54e8 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 194a6fcd..c56aac2f 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 */ -- GitLab