Skip to content

Instantly share code, notes, and snippets.

@sonicoder86
Last active August 9, 2016 13:11
Show Gist options
  • Save sonicoder86/beaada4ee83688da5e456c660a4ec8cd to your computer and use it in GitHub Desktop.
Save sonicoder86/beaada4ee83688da5e456c660a4ec8cd to your computer and use it in GitHub Desktop.
Upgrading to the new Angular 2 router - part 11
// 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