Last active
October 19, 2016 04:58
-
-
Save usmansaleem/bc27855fadb26edf8668be221b0e09ab to your computer and use it in GitHub Desktop.
Angular 2 AuthGuard
This file contains hidden or 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 { 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