Created
October 7, 2016 17:56
-
-
Save devisnotnull/e649c7dfe67c9bd8ce941835f80bf105 to your computer and use it in GitHub Desktop.
Adding guards to Angular 2 routes
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 { Router, CanActivate } from '@angular/router'; | |
import { Injectable } from '@angular/core'; | |
/** **/ | |
class UserToken {} | |
/** **/ | |
class Permissions { | |
canActivate(user: UserToken, id: string): boolean { | |
return true; | |
} | |
} | |
/** **/ | |
@Injectable() | |
class CanActivateTeam implements CanActivate { | |
constructor(private permissions: Permissions, private currentUser: UserToken) {} | |
canActivate( | |
route: ActivatedRouteSnapshot, | |
state: RouterStateSnapshot | |
): Observable<boolean>|Promise<boolean>|boolean { | |
return this.permissions.canActivate(this.currentUser, route.params.id); | |
} | |
} | |
@NgModule({ | |
imports: [ | |
RouterModule.forRoot([ | |
{ | |
path: 'team/:id', | |
component: TeamCmp, | |
canActivate: [CanActivateTeam] | |
} | |
]) | |
], | |
providers: [CanActivateTeam, UserToken, Permissions] | |
}) | |
class AppModule {} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Will enrich when prototype is complete