-
-
Save ashhitch/3cfe2de5bfd62ba6f6f5366306b756fe to your computer and use it in GitHub Desktop.
Upgrading to the new Angular 2 router - part 11
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
// 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