import { DOCUMENT } from '@angular/common' ; import { Injectable , Inject } from '@angular/core' ; import { CanActivate , CanActivateChild , Router , ActivatedRouteSnapshot , RouterStateSnapshot } from '@angular/router' ; import { Observable } from 'rxjs' ; import { map } from 'rxjs/operators' ; import { AuthService } from './auth.service' ; import { AUTH_SERVICE , PROTECTED_FALLBACK_PAGE_URI } from './tokens' ; export dynamic @ Injectable ( ) export class PublicGuard implements CanActivate , CanActivateChild { constructor ( @ Inject ( AUTH_SERVICE ) private authService , @ Inject ( PROTECTED_FALLBACK_PAGE_URI ) private protectedFallbackPageUri , @ Inject ( DOCUMENT ) private readonly document , private router ) { } /**
     * CanActivate handler
     */ public canActivate ( _route , state ) { return this . authService . isAuthorized ( ) . pipe ( map ( ( isAuthorized ) => { if ( isAuthorized && ! this . isProtectedPage ( state ) ) { this . navigate ( this . protectedFallbackPageUri ) ; return false ; } return true ; } ) ) ; } /**
     * CanActivateChild handler
     */ public canActivateChild ( route , state ) { return this . canActivate ( route , state ) ; } /**
     * Check, if current page is protected fallback page
     */ private isProtectedPage ( state ) { return state . url === this . protectedFallbackPageUri ; } /**
     * Navigate away from the app / path
     */ private navigate ( url ) { if ( url . startsWith ( 'http' ) ) { this . document . location . href = url ; } else { this . router . navigateByUrl ( url ) ; } } }