Skip to content

Commit ca332db

Browse files
committed
Add sign in redirect, closes #24
1 parent 709d27e commit ca332db

File tree

3 files changed

+23
-5
lines changed

3 files changed

+23
-5
lines changed

README.md

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,10 @@ Configuration options can be passed as `Angular2TokenOptions` via `.init()`.
8585
constructor(private _tokenService: Angular2TokenService) {
8686
this._tokenService.init({
8787
apiPath: null,
88+
8889
signInPath: 'auth/sign_in',
90+
signInRedirect: null,
91+
8992
signOutPath: 'auth/sign_out',
9093
validateTokenPath: 'auth/validate_token',
9194
@@ -106,6 +109,7 @@ constructor(private _tokenService: Angular2TokenService) {
106109
| ----------------------------------- | ----------------------------------------------- |
107110
| `apiPath?: string` | Sets base path all operations are based on |
108111
| `signInPath?: string` | Sets path for sign in |
112+
| `signInRedirect?: string` | Sets redirect path for failed CanActivate |
109113
| `signOutPath?: string` | Sets path for sign out |
110114
| `validateTokenPath?: string` | Sets path for token validation |
111115
| `registerAccountPath?: string` | Sets path for account registration |
@@ -285,8 +289,9 @@ this._tokenService.currentUser; // ADMIN
285289
```
286290
287291
## Route Guards
288-
Angular2-Token implements the `CanActivate` interface, so it can directly be used
289-
as a route guard. It currently does not distinguish between user types.
292+
Angular2-Token implements the `CanActivate` interface, so it can directly be used as a route guard.
293+
If the `signInRedirect` option is set the user will be redirected on a failed (=false) CanActivate using `Router.navigate()`.
294+
It currently does not distinguish between user types.
290295
291296
#### Example:
292297
```javascript

src/angular2-token.model.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,10 @@ export interface OAuthPaths {
1717

1818
export interface Angular2TokenOptions {
1919
apiPath?: string;
20+
2021
signInPath?: string;
22+
signInRedirect?: string;
23+
2124
signOutPath?: string;
2225
validateTokenPath?: string;
2326

src/angular2-token.service.ts

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import {
88
RequestMethod,
99
RequestOptions
1010
} from '@angular/http';
11-
import { ActivatedRoute } from '@angular/router';
11+
import { ActivatedRoute, Router } from '@angular/router';
1212
import { Observable } from 'rxjs/Observable';
1313
import 'rxjs/add/operator/share';
1414

@@ -39,22 +39,32 @@ export class Angular2TokenService implements CanActivate {
3939

4040
constructor(
4141
private _http: Http,
42-
private _activatedRoute: ActivatedRoute
42+
private _activatedRoute: ActivatedRoute,
43+
private _router: Router
4344
) { }
4445

4546
canActivate() {
4647
if (this._currentUserData)
4748
return true;
48-
else
49+
else {
50+
51+
// Redirect user to sign in if signInRedirect is set
52+
if(this._options.signInRedirect)
53+
this._router.navigate([this._options.signInRedirect]);
54+
4955
return false;
56+
}
5057
}
5158

5259
// Inital configuration
5360
init(options?: Angular2TokenOptions) {
5461

5562
let defaultOptions: Angular2TokenOptions = {
5663
apiPath: null,
64+
5765
signInPath: 'auth/sign_in',
66+
signInRedirect: null,
67+
5868
signOutPath: 'auth/sign_out',
5969
validateTokenPath: 'auth/validate_token',
6070

0 commit comments

Comments
 (0)