Last active
July 29, 2018 21:03
-
-
Save theoomoregbee/f059f30a0fc968f7495a12cf24e0395a to your computer and use it in GitHub Desktop.
Auth guard for guard post
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { AuthService } from './../services/auth.service'; | |
import { Injectable } from '@angular/core'; | |
import { CanActivate, ActivatedRouteSnapshot, RouterStateSnapshot, Router, Route } from '@angular/router'; | |
import { Observable } from 'rxjs'; | |
@Injectable() | |
export class AuthGuard implements CanActivate { | |
constructor(private _authService: AuthService, private _router: Router) { | |
} | |
canActivate(next: ActivatedRouteSnapshot, state: RouterStateSnapshot): Observable<boolean> | Promise<boolean> | boolean { | |
if (this._authService.isAuthenticated()) { | |
return true; | |
} | |
// navigate to login page | |
this._router.navigate(['/login']); | |
// you can save redirect url so after authing we can move them back to the page they requested | |
return false; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment