Skip to content

Commit

Permalink
Merge pull request #20 from Mangopay/bugfix/auth-token-renew
Browse files Browse the repository at this point in the history
Fix expired authorization token error
  • Loading branch information
hobailey authored Aug 2, 2016
2 parents 7b26fa7 + 00bd796 commit a7a9ee6
Showing 1 changed file with 9 additions and 1 deletion.
10 changes: 9 additions & 1 deletion lib/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ Api.prototype = {
/**
* If there's no OAuthKey, request one
*/
if (!this.requestOptions.headers.Authorization) {
if (!this.requestOptions.headers.Authorization || this.isExpired()) {
return new Promise(function(resolve, reject){
self.authorize(function(){
self.method.call(self, method, function(data, response){
Expand Down Expand Up @@ -255,10 +255,18 @@ Api.prototype = {
_.extend(self.requestOptions.headers, {
Authorization: data.token_type + ' ' + data.access_token
});
// Multiplying expires_in (seconds) by 1000 since JS getTime() is expressed in ms
self.authorizationExpireTime = new Date().getTime() + ( data.expires_in * 1000 );
callback(data);
});
},

isExpired: function() {
// Expressed in ms ( 10 seconds )
var THRESHOLD = 10000;
return (new Date().getTime() - THRESHOLD) > this.authorizationExpireTime;
},

/**
* Populates the SDK object with the services
*/
Expand Down

0 comments on commit a7a9ee6

Please sign in to comment.