Guest User

Untitled

a guest
Jan 24th, 2019
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.84 KB | None | 0 0
  1. import { AuthenticationService } from '../services/authentication.service';
  2. import { Injectable } from '@angular/core';
  3. import { CanActivate, ActivatedRouteSnapshot, RouterStateSnapshot, Router, Route } from '@angular/router';
  4. import { Observable } from 'rxjs';
  5.  
  6. @Injectable()
  7. export class AuthGuard implements CanActivate {
  8.  
  9.  
  10. constructor(private _authService:AuthenticationService , private _router: Router) {
  11. }
  12.  
  13. canActivate(next: ActivatedRouteSnapshot, state: RouterStateSnapshot): Observable<boolean> | Promise<boolean> | boolean {
  14. // implement check function in your auth service
  15. if (this._authService.isLoggedIn()) {
  16. return true;
  17. }
  18. // navigate to login page
  19. this._router.navigate(['/login']);
  20. // you can save redirect url so after authing we can move them back to the page they requested
  21. return false;
  22. }
  23.  
  24. }
Add Comment
Please, Sign In to add comment