File tree Expand file tree Collapse file tree 4 files changed +36
-5
lines changed Expand file tree Collapse file tree 4 files changed +36
-5
lines changed Original file line number Diff line number Diff line change 11# Change Log
22
3+ ## v0.1.6
4+ ** Bugfixes:**
5+ - Added route guard support for CanActivate
6+
37## v0.1.5
48** Bugfixes:**
59- Fix broken sourcemap paths in npm package
Original file line number Diff line number Diff line change @@ -31,6 +31,7 @@ The repository can be found [here](https://github.com/neroniaky/angular2-token-e
3131 - [ ` .resetPassword() ` ] ( #resetpassword )
3232- [ HTTP Service Wrapper] ( #http-service-wrapper )
3333- [ Multiple User Types] ( #multiple-user-types )
34+ - [ Route Guards] ( #route-guards )
3435- [ Testing] ( #testing )
3536- [ Credits] ( #credits )
3637- [ License] ( #license )
@@ -262,6 +263,24 @@ this._tokenService.signIn(
262263this._tokenService.currentUser; // ADMIN
263264` ` `
264265
266+ # # Route Guards
267+ Angular2-Token implements the ` CanActivate` interface, so it can directly be used
268+ as a route guard. It currently does not distinguish between user types.
269+
270+ # ### Example:
271+ ` ` ` javascript
272+ const routerConfig: Routes = [
273+ {
274+ path: ' ' ,
275+ component: PublicComponent
276+ }, {
277+ path: ' restricted' ,
278+ component: RestrictedComponent,
279+ canActivate: [Angular2TokenService]
280+ }
281+ ];
282+ ` ` `
283+
265284# # Testing
266285` ` ` bash
267286npm test
Original file line number Diff line number Diff line change 11{
22 "name" : " angular2-token" ,
3- "version" : " 0.1.5 " ,
3+ "version" : " 0.1.6 " ,
44 "description" : " Angular2 service for token based authentication" ,
55 "main" : " ./angular2-token.js" ,
66 "typings" : " ./angular2-token.d.ts" ,
Original file line number Diff line number Diff line change 1- import { Injectable } from '@angular/core' ;
1+ import { Injectable } from '@angular/core' ;
2+ import { CanActivate } from '@angular/router' ;
23import {
34 Http ,
45 Response ,
78 RequestMethod ,
89 RequestOptions
910} from '@angular/http' ;
10- import { ActivatedRoute } from '@angular/router' ;
11- import { Observable } from 'rxjs/Observable' ;
11+ import { ActivatedRoute } from '@angular/router' ;
12+ import { Observable } from 'rxjs/Observable' ;
1213import 'rxjs/add/operator/share' ;
1314
1415import {
@@ -18,7 +19,7 @@ import {
1819} from './angular2-token.model' ;
1920
2021@Injectable ( )
21- export class Angular2TokenService {
22+ export class Angular2TokenService implements CanActivate {
2223
2324 get currentUserType ( ) : string {
2425 if ( this . _currentUserType != null )
@@ -41,6 +42,13 @@ export class Angular2TokenService {
4142 private _activatedRoute : ActivatedRoute
4243 ) { }
4344
45+ canActivate ( ) {
46+ if ( this . _currentUserData )
47+ return true ;
48+ else
49+ return false ;
50+ }
51+
4452 // Inital configuration
4553 init ( options ?: Angular2TokenOptions ) {
4654
You can’t perform that action at this time.
0 commit comments