SHARE
TWEET

Untitled

a guest Sep 22nd, 2019 81 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import { Injectable } from '@angular/core';
  2. import { Router, CanActivate, ActivatedRouteSnapshot, RouterStateSnapshot } from '@angular/router';
  3. import { AuthenticationService } from 'app/_services';
  4.  
  5. @Injectable({ providedIn: 'root' })
  6. export class AuthGuard implements CanActivate {
  7.   constructor(private router: Router, private authenticationService: AuthenticationService) {}
  8.  
  9.   canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot) {
  10.     const currentUser = this.authenticationService.currentUserValue;
  11.     if (currentUser) {
  12.       // authorised so return true
  13.       return true;
  14.     }
  15.  
  16.     // not logged in so redirect to login page with the return url
  17.     this.router.navigate(['/login'], { queryParams: { returnUrl: state.url } });
  18.     return false;
  19.   }
  20. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top