Skip to content

Commit 9642263

Browse files
committed
Merge pull request #45 from sgruhier/redirectto
add signInStoredUrlStorageKey option
2 parents c3a61ad + 5e20d76 commit 9642263

File tree

3 files changed

+42
-15
lines changed

3 files changed

+42
-15
lines changed

README.md

Lines changed: 33 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@ constructor(private _tokenService: Angular2TokenService) {
9393
9494
signInPath: 'auth/sign_in',
9595
signInRedirect: null,
96+
signInStoredUrlStorageKey: null,
9697
9798
signOutPath: 'auth/sign_out',
9899
validateTokenPath: 'auth/validate_token',
@@ -117,21 +118,22 @@ constructor(private _tokenService: Angular2TokenService) {
117118
}
118119
```
119120
120-
| Options | Description |
121-
| ----------------------------------- | ----------------------------------------------- |
122-
| `apiPath?: string` | Sets base path all operations are based on |
123-
| `signInPath?: string` | Sets path for sign in |
124-
| `signInRedirect?: string` | Sets redirect path for failed CanActivate |
125-
| `signOutPath?: string` | Sets path for sign out |
126-
| `validateTokenPath?: string` | Sets path for token validation |
127-
| `registerAccountPath?: string` | Sets path for account registration |
128-
| `deleteAccountPath?: string` | Sets path for account deletion |
129-
| `registerAccountCallback?: string` | Sets the path user are redirected to after email confirmation for registration |
130-
| `updatePasswordPath?: string` | Sets path for password update |
131-
| `resetPasswordPath?: string` | Sets path for password reset |
132-
| `resetPasswordCallback?: string` | Sets the path user are redirected to after email confirmation for password reset |
133-
| `userTypes?: UserTypes[]` | Allows the configuration of multiple user types (see [Multiple User Types](#multiple-user-types)) |
134-
| `globalOptions?: GlobalOptions` | Allows the configuration of global options (see below) |
121+
| Options | Description |
122+
| --------------------------------------- | ---------------------------------------- |
123+
| `apiPath?: string` | Sets base path all operations are based on |
124+
| `signInPath?: string` | Sets path for sign in |
125+
| `signInRedirect?: string` | Sets redirect path for failed CanActivate |
126+
| `signInStoredUrlStorageKey?: string` | Sets locale storage key to store URL before displaying signIn page |
127+
| `signOutPath?: string` | Sets path for sign out |
128+
| `validateTokenPath?: string` | Sets path for token validation |
129+
| `registerAccountPath?: string` | Sets path for account registration |
130+
| `deleteAccountPath?: string` | Sets path for account deletion |
131+
| `registerAccountCallback?: string` | Sets the path user are redirected to after email confirmation for registration |
132+
| `updatePasswordPath?: string` | Sets path for password update |
133+
| `resetPasswordPath?: string` | Sets path for password reset |
134+
| `resetPasswordCallback?: string` | Sets the path user are redirected to after email confirmation for password reset |
135+
| `userTypes?: UserTypes[]` | Allows the configuration of multiple user types (see [Multiple User Types](#multiple-user-types)) |
136+
| `globalOptions?: GlobalOptions` | Allows the configuration of global options (see below) |
135137
136138
### Global Options
137139
| Options | Description |
@@ -349,6 +351,22 @@ Returns current authentication data which are used to set auth headers.
349351
350352
`get currentAuthData(): AuthData`
351353
354+
### Redirect original requested URL
355+
If you want to redirect to the protected URL after signing in, you need to set `signInStoredUrlStorageKey` and in your code you can do something like this
356+
357+
```js
358+
this._tokenService.signIn(
359+
360+
'secretPassword'
361+
).subscribe(
362+
res => {
363+
// You have to add Router DI in your component
364+
this.router.navigateByUrl(localStorage.getItem('redirectTo'));
365+
},
366+
error => console.log(error)
367+
);
368+
```
369+
352370
## Development
353371
If the package is installed from Github specified in the package.json, you need to build the package locally.
354372

src/angular2-token.model.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ export interface Angular2TokenOptions {
3636

3737
signInPath?: string;
3838
signInRedirect?: string;
39+
signInStoredUrlStorageKey?: string;
3940

4041
signOutPath?: string;
4142
validateTokenPath?: string;

src/angular2-token.service.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,13 @@ export class Angular2TokenService implements CanActivate {
5656
if (this.userSignedIn())
5757
return true;
5858
else {
59+
// Store current location in storage (usefull for redirection after signing in)
60+
if (this._options.signInStoredUrlStorageKey) {
61+
localStorage.setItem(
62+
this._options.signInStoredUrlStorageKey,
63+
window.location.pathname + window.location.search
64+
);
65+
}
5966

6067
// Redirect user to sign in if signInRedirect is set
6168
if(this._options.signInRedirect)
@@ -73,6 +80,7 @@ export class Angular2TokenService implements CanActivate {
7380

7481
signInPath: 'auth/sign_in',
7582
signInRedirect: null,
83+
signInStoredUrlStorageKey: null,
7684

7785
signOutPath: 'auth/sign_out',
7886
validateTokenPath: 'auth/validate_token',

0 commit comments

Comments
 (0)