Skip to content

Instantly share code, notes, and snippets.

@theoomoregbee
Last active July 29, 2018 21:03
Show Gist options
  • Save theoomoregbee/f059f30a0fc968f7495a12cf24e0395a to your computer and use it in GitHub Desktop.
Save theoomoregbee/f059f30a0fc968f7495a12cf24e0395a to your computer and use it in GitHub Desktop.
Auth guard for guard post
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