import { Injectable } from '@angular/core'; import { Router } from '@angular/router'; import { Observable } from 'rxjs'; import { map } from 'rxjs/operators'; import { CanActivate, ActivatedRouteSnapshot, RouterStateSnapshot } from '@angular/router'; import { AuthService } from '../services/auth.service'; @Injectable({ providedIn: 'root' }) export class AuthUpdateGuard implements CanActivate { constructor(private authService: AuthService) { } canActivate(next: ActivatedRouteSnapshot, state: RouterStateSnapshot) { this.authService.getAuth().catch(function (error) { }); return true; } } @Injectable({ providedIn: 'root' }) export class AuthGuard implements CanActivate { constructor(private authService: AuthService, private router: Router) { } canActivate(next: ActivatedRouteSnapshot, state: RouterStateSnapshot): Promise { const that = this; return this.authService.getAuth().then(response => { return true; }).catch(function (error) { that.router.navigateByUrl('/unavailable'); return false; }); } } @Injectable({ providedIn: 'root' }) export class AuthenticatedGuard implements CanActivate { constructor(private authService: AuthService, private router: Router) { } canActivate(next: ActivatedRouteSnapshot, state: RouterStateSnapshot): Promise { const that = this; return this.authService.getAuth().then((data: any) => { if (!data.authenticated) { this.router.navigateByUrl('/login'); return false; } return true; }).catch(function (error) { that.router.navigateByUrl('/unavailable'); return false; }); } } @Injectable({ providedIn: 'root' }) export class AnonymousGuard implements CanActivate { constructor(private authService: AuthService, private router: Router) { } canActivate(next: ActivatedRouteSnapshot, state: RouterStateSnapshot): Promise { const that = this; return this.authService.getAuth().then((data: any) => { if (data.authenticated) { this.router.navigateByUrl('/account/info'); return false; } return true; }).catch(function (error) { that.router.navigateByUrl('/unavailable'); return false; }); } }