import { Injectable } from '@angular/core' ; import { CanActivate , ActivatedRouteSnapshot , RouterStateSnapshot , Params , Router } from '@angular/router' ; import { Observable , of } from 'rxjs' ; import { take , switchMap , filter , concatMap , first } from 'rxjs/operators' ; import { OidcFacade } from 'ng-oidc-client' ; import { User } from 'oidc-client' ; @ Injectable ( { providedIn : 'root' } ) export class OidcGuardService implements CanActivate { constructor ( private router , private oidcFacade ) { } public canActivate ( route , state ) { return this . oidcFacade . waitForAuthenticationLoaded ( ) . pipe ( switchMap ( loading => { return this . hasAuthenticatedUser ( ) ; } ) ) ; } /**
     * Check to see oidc identity exists in state
     */ hasAuthenticatedUser ( ) { return this . oidcFacade . identity$ . pipe ( first ( ) , switchMap ( user => { if ( user && ! user . expired ) { return of ( true ) ; } else { this . router . navigate ( [ '/unauthorized' ] ) ; return of ( false ) ; } } ) ) ; } }