Last active
August 9, 2016 13:11
-
-
Save sonicoder86/beaada4ee83688da5e456c660a4ec8cd to your computer and use it in GitHub Desktop.
Upgrading to the new Angular 2 router - part 11
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
// auth-guard.ts | |
import { Injectable } from '@angular/core'; | |
import { | |
CanActivate, | |
Router, | |
ActivatedRouteSnapshot, | |
RouterStateSnapshot | |
} from '@angular/router'; | |
import { AuthService } from './services/auth/auth.service'; | |
@Injectable() | |
export class AuthGuard implements CanActivate { | |
constructor(private authService: AuthService, private router: Router) {} | |
canActivate(next: ActivatedRouteSnapshot, state: RouterStateSnapshot) { | |
if (this.authService.isLoggedIn()) { | |
return true; | |
} | |
this.router.navigate(['login']); | |
return false; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment