Skip to content

Instantly share code, notes, and snippets.

@usmansaleem
Last active October 19, 2016 04:58
Show Gist options
  • Save usmansaleem/bc27855fadb26edf8668be221b0e09ab to your computer and use it in GitHub Desktop.
Save usmansaleem/bc27855fadb26edf8668be221b0e09ab to your computer and use it in GitHub Desktop.
Angular 2 AuthGuard
import { Injectable } from '@angular/core';
import { Router, CanActivate } from '@angular/router';
import {LoginService} from "./login/login.service";
@Injectable()
export class AuthGuard implements CanActivate {
constructor(private router: Router,
private loginService: LoginService) { }
canActivate() {
if (this.loginService.isLoggedIn()) {
// logged in so return true
return true;
}
// not logged in so redirect to login page
this.router.navigate(['/login']);
return false;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment