Skip to content
This repository has been archived by the owner on Nov 25, 2020. It is now read-only.

Commit

Permalink
feat(fetchClientConfig): logout when token is invalidated by server
Browse files Browse the repository at this point in the history
  • Loading branch information
VMBindraban committed May 5, 2017
1 parent 8f83408 commit ad9b66a
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 1 deletion.
4 changes: 3 additions & 1 deletion doc/baseConfig.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,8 @@ unlinkMethod = 'get';
authHeader = 'Authorization';
// The token name used in the header of API requests that require authentication
authTokenType = 'Bearer';
// Logout when the token is invalidated by the server
logoutOnInvalidtoken = false;
// The property from which to get the access token after a successful login or signup
accessTokenProp = 'access_token';

Expand Down Expand Up @@ -127,7 +129,7 @@ storage = 'localStorage';
storageKey = 'aurelia_authentication';
// full page reload if authorization changed in another tab (recommended to set it to 'true')
storageChangedReload = false;
// optional function to extract the expiration date. Takes the server response as parameter and returns NumericDate = number of seconds! since 1 January 1970 00:00:00 UTC (Unix Epoch)
// optional function to extract the expiration date. Takes the server response as parameter and returns NumericDate = number of seconds! since 1 January 1970 00:00:00 UTC (Unix Epoch)
// eg (expires_in in sec): getExpirationDateFromResponse = serverResponse => new Date().getTime() / 1000 + serverResponse.expires_in;
getExpirationDateFromResponse = null;
// optional function to extract the access token from the response. Takes the server response as parameter and returns a token
Expand Down
2 changes: 2 additions & 0 deletions src/baseConfig.js
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,8 @@ export class BaseConfig {
authHeader = 'Authorization';
// The token name used in the header of API requests that require authentication
authTokenType = 'Bearer';
// Logout when the token is invalidated by the server
logoutOnInvalidtoken = false;
// The the property from which to get the access token after a successful login or signup. Can also be dotted eg "accessTokenProp.accessTokenName"
accessTokenProp = 'access_token';

Expand Down
4 changes: 4 additions & 0 deletions src/fetchClientConfig.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,10 @@ export class FetchConfig {
if (response.status !== 401) {
return resolve(response);
}
// logout when server invalidated the authorization token but the token itself is still valid
if (this.config.httpInterceptor && this.config.logoutOnInvalidtoken && !this.authService.isTokenExpired()) {
return reject(this.authService.logout());
}
// resolve unexpected authorization errors (not a managed request or token not expired)
if (!this.config.httpInterceptor || !this.authService.isTokenExpired()) {
return resolve(response);
Expand Down

0 comments on commit ad9b66a

Please sign in to comment.