all files / src/ open-id-connect-user-block.ts

82.61% Statements 19/23
100% Branches 2/2
75% Functions 6/8
88.89% Lines 16/18
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37                                          
import { autoinject, customElement } from 'aurelia-framework';
import { User } from 'oidc-client';
import OpenIdConnect from './open-id-connect';
 
@autoinject
@customElement('open-id-connect-user-block')
export default class {
 
  protected user: User | null = null;
 
  public get isLoggedIn(): boolean {
    return this.user !== null && this.user !== undefined;
  }
 
  constructor(protected openIdConnect: OpenIdConnect) { }
 
  public async attached() {
    this.openIdConnect.addOrRemoveHandler('addUserUnloaded', () => {
      this.user = null;
    });
 
    this.openIdConnect.addOrRemoveHandler('addUserLoaded', async () => {
      this.user = await this.openIdConnect.getUser();
    });
 
    this.user = await this.openIdConnect.getUser();
  }
 
  public login() {
    this.openIdConnect.login();
  }
 
  public logout() {
    this.openIdConnect.logout();
  }
}