Advertisement
Guest User

Untitled

a guest
Jul 17th, 2018
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import { CanActivate, Router, CanLoad, Route, ActivatedRouteSnapshot, RouterStateSnapshot, CanActivateChild } from '@angular/router';
  2. import { Injectable } from '@angular/core';
  3. import { AuthService } from './auth.service';
  4. import { Observable } from 'rxjs';
  5.  
  6. @Injectable({
  7.     providedIn: 'root'
  8. })
  9. export class AuthGuard implements CanActivate, CanActivateChild, CanLoad {
  10.  
  11.     constructor(private authService: AuthService, private router: Router) {
  12.  
  13.     }
  14.  
  15.     checkAuth() {
  16.         if ( this.authService.isAuth() ) {
  17.             return true;
  18.         }
  19.         else {
  20.             this.router.navigate(['login']);
  21.         }
  22.     }
  23.  
  24.     canLoad(route: Route): Observable<boolean>|Promise<boolean>|boolean {
  25.         return this.checkAuth();
  26.     }
  27.  
  28.     canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot) {
  29.         return this.checkAuth();
  30.     }
  31.  
  32.     canActivateChild(childRoute: ActivatedRouteSnapshot, state: RouterStateSnapshot): Observable<boolean> | Promise<boolean> | boolean {
  33.         return this.checkAuth();
  34.     }
  35.  
  36. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement