import { Injectable } from '@angular/core' ; import { ActivatedRouteSnapshot , CanActivate , RouterStateSnapshot } from '@angular/router' ; import { Store } from '@ngxs/store' ; import { Observable } from 'rxjs' ; import { map } from 'rxjs/operators' ; import { AuthState , MustLogin } from '../stores/auth.state' ; @ Injectable ( { providedIn : 'root' , } ) export class AuthGuard implements CanActivate { constructor ( private store ) { } canActivate ( next , state ) { return this . store . selectOnce ( AuthState . isAuthenticated ) . pipe ( map ( ok => { if ( ! ok ) { this . store . dispatch ( new MustLogin ( state . url ) ) ; } return true ; } ) ) ; } }