import {Injectable} from '@angular/core'; import {Router} from '@angular/router'; 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) { const that = this; return this.authService.getAuth().then(response => { return true; }).catch(function(error) { return that.router.parseUrl('/unavailable'); }); } } @Injectable({ providedIn: 'root' }) export class AuthenticatedGuard implements CanActivate { constructor(private authService: AuthService, private router: Router) {} canActivate(next: ActivatedRouteSnapshot, state: RouterStateSnapshot) { const that = this; return this.authService.getAuth().then((data: any) => { if(!data.authenticated) { this.router.navigateByUrl('/login'); return false; } return true; }).catch(function(error) { return that.router.parseUrl('/unavailable'); }); } } @Injectable({ providedIn: 'root' }) export class AnonymousGuard implements CanActivate { constructor(private authService: AuthService, private router: Router) {} canActivate(next: ActivatedRouteSnapshot, state: RouterStateSnapshot) { 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) { return that.router.parseUrl('/unavailable'); }); } }