import { Injectable } from '@angular/core' ; import { CanActivate } from '@angular/router' ; import { Observable } from 'rxjs' ; import { first , mapTo , switchMap , tap } from 'rxjs/operators' ; import { AuthFacade } from '@pimp-my-pr/pmp-web/auth/data-access' ; import { User } from '@pimp-my-pr/shared/domain' ; import { AuthPublicFacade } from '../+state/auth-public.facade' ; @ Injectable ( { providedIn : 'root' } ) export class AuthPublicGuard implements CanActivate { constructor ( private authFacade , private authPublicFacade ) { } canActivate ( ) { return this . authFacade . authToken$ . pipe ( tap ( ( token ) => { if ( ! token ) { this . authPublicFacade . loginRememberedUserOrGoToLogin ( ) ; } } ) , first ( ( token ) => ! ! token ) , switchMap ( ( ) => this . authPublicFacade . user$ ) , tap ( ( user ) => { if ( ! user ) { this . authPublicFacade . getUser ( ) ; } } ) , first ( ( user ) => ! ! user ) , mapTo ( true ) ) ; } }