import { Injectable } from '@angular/core'; import { CanActivate, ActivatedRouteSnapshot, RouterStateSnapshot, UrlTree, Router } from '@angular/router'; import { Store, select } from '@ngrx/store'; import { Observable } from 'rxjs'; import { map, take } from 'rxjs/operators'; import { AuthenticationService } from '../services/authentication.service'; @Injectable({ providedIn: 'root' }) export class AuthGuard implements CanActivate { constructor( private router: Router, private authenticationService: AuthenticationService ) {} canActivate( route: ActivatedRouteSnapshot, state: RouterStateSnapshot ): | boolean | UrlTree | Observable | Promise { return new Promise((resolve, reject) => { if (this.authenticationService.authenticated()) { resolve(true); } else { this.router.navigateByUrl('/account/login'); resolve(false); } }); } }