import { Injectable } from '@angular/core'; import { CanActivate, ActivatedRouteSnapshot, RouterStateSnapshot, } from '@angular/router'; import { map, take } from 'rxjs/operators'; import { MwAuthService } from '../auth.service'; import { MwUserService } from '../user.service'; import { combineLatest, Observable } from 'rxjs'; @Injectable() export class AnonymousGuard implements CanActivate { constructor( private authService: MwAuthService, private userService: MwUserService ) {} canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): Observable { const loggedIn$ = this.authService.loggedIn$.pipe(take(1)); const profile$ = this.userService.userProfile$.pipe(take(1)); return combineLatest([loggedIn$, profile$]).pipe( map(([loggedIn, profile]) => { if (!loggedIn || !profile) { return true; } return false; }) ); } }