Skip to content

Commit 4870132

Browse files
committed
Added route guard support
1 parent bad7038 commit 4870132

File tree

4 files changed

+36
-5
lines changed

4 files changed

+36
-5
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
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

README.md

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff 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(
262263
this._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
267286
npm test

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
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",

src/angular2-token.service.ts

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
import { Injectable } from '@angular/core';
1+
import { Injectable } from '@angular/core';
2+
import { CanActivate } from '@angular/router';
23
import {
34
Http,
45
Response,
@@ -7,8 +8,8 @@ import {
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';
1213
import 'rxjs/add/operator/share';
1314

1415
import {
@@ -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

0 commit comments

Comments
 (0)