Skip to content

Commit b58e81e

Browse files
authored
Merge pull request #64 from cybey/signOutFailedValidate
Added functionality to trigger the signOut function when service retu…
2 parents 1ad660f + 6da70d4 commit b58e81e

File tree

4 files changed

+35
-1
lines changed

4 files changed

+35
-1
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,7 @@ constructor(private _tokenService: Angular2TokenService) {
9797
9898
signOutPath: 'auth/sign_out',
9999
validateTokenPath: 'auth/validate_token',
100+
signOutFailedValidate: false,
100101
101102
registerAccountPath: 'auth',
102103
deleteAccountPath: 'auth',
@@ -126,6 +127,7 @@ constructor(private _tokenService: Angular2TokenService) {
126127
| `signInStoredUrlStorageKey?: string` | Sets locale storage key to store URL before displaying signIn page |
127128
| `signOutPath?: string` | Sets path for sign out |
128129
| `validateTokenPath?: string` | Sets path for token validation |
130+
| `signOutFailedValidate?: boolean` | Signs user out when validation returns a 401 status |
129131
| `registerAccountPath?: string` | Sets path for account registration |
130132
| `deleteAccountPath?: string` | Sets path for account deletion |
131133
| `registerAccountCallback?: string` | Sets the path user are redirected to after email confirmation for registration |

src/angular2-token.model.ts

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

7070
signOutPath?: string;
7171
validateTokenPath?: string;
72+
signOutFailedValidate?: boolean;
7273

7374
deleteAccountPath?: string;
7475
registerAccountPath?: string;

src/angular2-token.service.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,7 @@ export class Angular2TokenService implements CanActivate {
9090

9191
signOutPath: 'auth/sign_out',
9292
validateTokenPath: 'auth/validate_token',
93+
signOutFailedValidate: false,
9394

9495
registerAccountPath: 'auth',
9596
deleteAccountPath: 'auth',
@@ -194,7 +195,13 @@ export class Angular2TokenService implements CanActivate {
194195
validateToken(): Observable<Response> {
195196
let observ = this.get(this._constructUserPath() + this._options.validateTokenPath);
196197

197-
observ.subscribe(res => this._currentUserData = res.json().data, error => null);
198+
observ.subscribe(
199+
res => this._currentUserData = res.json().data,
200+
error => {
201+
if (error.status === 401 && this._options.signOutFailedValidate) {
202+
this.signOut();
203+
}
204+
});
198205

199206
return observ;
200207
}

src/angular2-token.spec.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,30 @@ describe('Angular2TokenService', () => {
180180
tokenService.validateToken();
181181
}));
182182

183+
it('validateToken should call signOut when it returns status 401', inject([Angular2TokenService, MockBackend], (tokenService, mockBackend) => {
184+
185+
mockBackend.connections.subscribe(
186+
c => c.mockError(new Response(new ResponseOptions({ status: 401, headers: new Headers() })))
187+
);
188+
189+
spyOn(tokenService, 'signOut');
190+
191+
tokenService.init({ apiPath: 'myapi', signOutFailedValidate: true });
192+
tokenService.validateToken().subscribe(res => null, err => expect(tokenService.signOut).toHaveBeenCalled());
193+
}));
194+
195+
it('validateToken should not call signOut when it returns status 401', inject([Angular2TokenService, MockBackend], (tokenService, mockBackend) => {
196+
197+
mockBackend.connections.subscribe(
198+
c => c.mockError(new Response(new ResponseOptions({ status: 401, headers: new Headers() })))
199+
);
200+
201+
spyOn(tokenService, 'signOut');
202+
203+
tokenService.init({ apiPath: 'myapi', signOutFailedValidate: false });
204+
tokenService.validateToken().subscribe(res => null, err => expect(tokenService.signOut).not.toHaveBeenCalled());
205+
}));
206+
183207
it('updatePasswordPath should send to configured path', inject([Angular2TokenService, MockBackend], (tokenService, mockBackend) => {
184208

185209
mockBackend.connections.subscribe(

0 commit comments

Comments
 (0)