Skip to content
Snippets Groups Projects
dbp-auth-demo.js 4.64 KiB
Newer Older
import {createInstance} from './i18n.js';
import {html} from 'lit';
import {ScopedElementsMixin} from '@open-wc/scoped-elements';
import {AuthKeycloak} from './auth-keycloak.js';
import {LoginButton} from './login-button.js';
import * as commonUtils from '@dbp-toolkit/common/utils';
//import {name as pkgName} from './../package.json';
Reiter, Christoph's avatar
Reiter, Christoph committed
import DBPLitElement from '@dbp-toolkit/common/dbp-lit-element';
export class DbpAuthDemo extends ScopedElementsMixin(DBPLitElement) {
        this.entryPointUrl = '';
        this.noAuth = false;
        this._i18n = createInstance();
    static get scopedElements() {
        return {
Reiter, Christoph's avatar
Reiter, Christoph committed
            'dbp-auth-keycloak': AuthKeycloak,
            'dbp-login-button': LoginButton,
        return {
            ...super.properties,
Reiter, Christoph's avatar
Reiter, Christoph committed
            lang: {type: String},
            entryPointUrl: {type: String, attribute: 'entry-point-url'},
            auth: {type: Object},
            noAuth: {type: Boolean, attribute: 'no-auth'},
    update(changedProperties) {
        changedProperties.forEach((oldValue, propName) => {
Reiter, Christoph's avatar
Reiter, Christoph committed
            if (propName === 'lang') {
                this._i18n.changeLanguage(this.lang);

        super.update(changedProperties);
        const div = this._('#person-info');
        if (!this.auth.token) {
Reiter, Christoph's avatar
Reiter, Christoph committed
            console.error('not logged in');
            div.innerHTML = 'You are not logged in!';
Reiter, Christoph's avatar
Reiter, Christoph committed
        let userInfoURL =
            'https://auth-dev.tugraz.at/auth/realms/tugraz-vpu/protocol/openid-connect/userinfo';

        // NOTE: the URL and realm need to match the keycloak config above
Reiter, Christoph's avatar
Reiter, Christoph committed
        const response = await fetch(userInfoURL, {
            headers: {
                'Content-Type': 'application/json',
                Authorization: 'Bearer ' + this.auth.token,
            },
        });
        const person = await response.json();
        console.log(person);
        div.innerHTML = JSON.stringify(person);
        const div = this._('#token-info');
        if (!this.auth.token) {
Reiter, Christoph's avatar
Reiter, Christoph committed
            console.error('not logged in');
            div.innerHTML = 'You are not logged in!';
        console.log(this.auth.token);
        div.innerHTML = this.auth.token;
    getAuthComponentHtml() {
Reiter, Christoph's avatar
Reiter, Christoph committed
        return this.noAuth
            ? html`
                  <dbp-login-button subscribe="auth" lang="${this.lang}"></dbp-login-button>
              `
            : html`
                  <div class="container">
                      <dbp-auth-keycloak
                          subscribe="requested-login-status,silent-check-sso-redirect-uri,url,realm,client-id,entry-point-url"
Reiter, Christoph's avatar
Reiter, Christoph committed
                          lang="${this.lang}"
                          entry-point-url="${this.entryPointUrl}"
                          try-login></dbp-auth-keycloak>
                      <dbp-login-button subscribe="auth" lang="${this.lang}"></dbp-login-button>
                  </div>
              `;
Neuber, Eugen Ramon's avatar
Neuber, Eugen Ramon committed
            <style>
Reiter, Christoph's avatar
Reiter, Christoph committed
                /* from BULMA.CSS */
Neuber, Eugen Ramon's avatar
Neuber, Eugen Ramon committed
                .section {
Reiter, Christoph's avatar
Reiter, Christoph committed
                    padding: 3rem 1.5rem;
                    font-family: sans-serif;
Neuber, Eugen Ramon's avatar
Neuber, Eugen Ramon committed
                }
                .content h1 {
                    font-size: 2em;
Reiter, Christoph's avatar
Reiter, Christoph committed
                    margin-bottom: 0.5em;
Reiter, Christoph's avatar
Reiter, Christoph committed
                .content h1,
                .content h2,
                .content h3,
                .content h4,
                .content h5,
                .content h6 {
                    color: var(--dbp-content);
Neuber, Eugen Ramon's avatar
Neuber, Eugen Ramon committed
                    font-weight: 600;
                    line-height: 1.125;
                }
Neuber, Eugen Ramon's avatar
Neuber, Eugen Ramon committed
            </style>
            <section class="section">
                <div class="container">
                    <h1 class="title">Auth-Demo</h1>
                </div>
                ${this.getAuthComponentHtml()}
                <div class="container">
Reiter, Christoph's avatar
Reiter, Christoph committed
                    <input type="button" value="Fetch userinfo" @click="${this._onUserInfoClick}" />
                    <input type="button" value="Show token" @click="${this._onShowToken}" />
                    <h4>Person info:</h4>
                    <div id="person-info"></div>
                    <h4>Token info:</h4>
                    <div id="token-info"></div>
commonUtils.defineCustomElement('dbp-auth-demo', DbpAuthDemo);