Advertisement
Guest User

Untitled

a guest
Sep 22nd, 2019
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.78 KB | None | 0 0
  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. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement