All files / src/app/core/identity-provider icm.identity-provider.ts

78.12% Statements 25/32
73.91% Branches 17/23
64.28% Functions 9/14
78.12% Lines 25/32

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82  2x 2x 2x 2x 2x   2x 2x 2x 2x         2x   4x 4x 4x 4x 4x                       4x   4x                     1x         2x 2x   2x   2x   2x   2x 2x                                        
import { HttpEvent, HttpHandler, HttpRequest } from '@angular/common/http';
import { Injectable } from '@angular/core';
import { ActivatedRouteSnapshot, Router } from '@angular/router';
import { Store, select } from '@ngrx/store';
import { Observable, noop } from 'rxjs';
import { filter, map, switchMap, take, tap } from 'rxjs/operators';
 
import { AccountFacade } from 'ish-core/facades/account.facade';
import { TokenService } from 'ish-core/services/token/token.service';
import { selectQueryParam } from 'ish-core/store/core/router';
import { ApiTokenService } from 'ish-core/utils/api-token/api-token.service';
 
import { IdentityProvider, TriggerReturnType } from './identity-provider.interface';
 
@Injectable({ providedIn: 'root' })
export class ICMIdentityProvider implements IdentityProvider {
  constructor(
    private router: Router,
    private store: Store,
    private apiTokenService: ApiTokenService,
    private accountFacade: AccountFacade,
    private tokenService: TokenService
  ) {}
 
  getCapabilities() {
    return {
      editPassword: true,
      editEmail: true,
      editProfile: true,
    };
  }
 
  init() {
    this.apiTokenService.restore$().subscribe(noop);
 
    this.apiTokenService.getCookieVanishes$().subscribe(type => {
      this.accountFacade.logoutUser({ revokeApiToken: false });
      Iif (type === 'user') {
        this.router.navigate(['/login'], {
          queryParams: { returnUrl: this.router.url, messageKey: 'session_timeout' },
        });
      }
    });
  }
 
  triggerLogin(): TriggerReturnType {
    return true;
  }
 
  triggerLogout(): TriggerReturnType {
    // revoke current token
    this.accountFacade.logoutUser();
    return this.accountFacade.isLoggedIn$.pipe(
      // wait until the user is logged out before you go to homepage to prevent unnecessary REST calls
      filter(loggedIn => !loggedIn),
      take(1),
      tap(() => this.tokenService.logOut()), // remove token from storage when user is logged out
      switchMap(() =>
        this.store.pipe(
          select(selectQueryParam('returnUrl')),
          map(returnUrl => returnUrl || '/home'),
          map(returnUrl => this.router.parseUrl(returnUrl))
        )
      )
    );
  }
 
  triggerRegister(): TriggerReturnType {
    return true;
  }
 
  triggerInvite(route: ActivatedRouteSnapshot): TriggerReturnType {
    return this.router.createUrlTree(['forgotPassword', 'updatePassword'], {
      queryParams: { uid: route.queryParams.uid, Hash: route.queryParams.Hash },
    });
  }
 
  intercept(req: HttpRequest<unknown>, next: HttpHandler): Observable<HttpEvent<unknown>> {
    return this.apiTokenService.intercept(req, next);
  }
}