Skip to content

Commit

Permalink
Merge pull request #44 from erichonkanen/master
Browse files Browse the repository at this point in the history
Fixes jwt authenticator restore method to schedule access token refresh after page reload
  • Loading branch information
jpadilla committed Apr 18, 2015
2 parents 39fa625 + 3554e33 commit 4b55b11
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 17 deletions.
29 changes: 15 additions & 14 deletions addon/authenticators/jwt.js
Original file line number Diff line number Diff line change
Expand Up @@ -103,22 +103,21 @@ export default TokenAuthenticator.extend({
dataObject = Ember.Object.create(data);

return new Ember.RSVP.Promise(function(resolve, reject) {
var now = (new Date()).getTime(),
expiresAt = _this.resolveTime(dataObject.get('expiresAt')),
token = dataObject.get(_this.tokenPropertyName);
var now = (new Date()).getTime();
var expiresAt = _this.resolveTime(dataObject.get(_this.tokenExpireName));
var token = dataObject.get(_this.tokenPropertyName);

if (Ember.isEmpty(token)) {
return reject(new Error('empty token'));
}

if (Ember.isEmpty(expiresAt)) {
if (Ember.isEmpty(expiresAt)) {
// Fetch the expire time from the token data since `expiresAt`
// wasn't included in the data object that was passed in.
var tokenData = _this.getTokenData(data[_this.tokenPropertyName]);
expiresAt = _this.resolveTime(tokenData[_this.tokenExpireName]);
if (Ember.isEmpty(expiresAt)) {
return resolve(data);
}

}
if (expiresAt !== expiresAt) {
return reject(new Error('invalid expiration'));
Expand All @@ -127,11 +126,12 @@ export default TokenAuthenticator.extend({
var wait = expiresAt - now - (_this.refreshLeeway * 1000);
if (wait > 0) {
if (_this.refreshAccessTokens) {
_this.scheduleAccessTokenRefresh(expiresAt, token);
_this.scheduleAccessTokenRefresh(dataObject.get(_this.tokenExpireName), token);
}
resolve(data);
} else if (_this.refreshAccessTokens) {
resolve(_this.refreshAccessToken(data.token).then(function () {
resolve(_this.refreshAccessToken(
dataObject.get(_this.tokenPropertyName)).then(function () {
return data;
}));
} else {
Expand Down Expand Up @@ -240,14 +240,15 @@ export default TokenAuthenticator.extend({
return new Ember.RSVP.Promise(function(resolve, reject) {
_this.makeRequest(_this.serverTokenRefreshEndpoint, data).then(function(response) {
Ember.run(function() {
var tokenData = _this.getTokenData(response[_this.tokenPropertyName]),
expiresAt = tokenData[_this.tokenExpireName],
data = Ember.merge(response, {
expiresAt: expiresAt
});
var tokenData = _this.getTokenData(response[_this.tokenPropertyName]);
var expiresAt = tokenData[_this.tokenExpireName];
var tokenExpireData = {};

_this.scheduleAccessTokenRefresh(expiresAt, response.token);
tokenExpireData[_this.tokenExpireName] = expiresAt;

data = Ember.merge(response, tokenExpireData);

_this.scheduleAccessTokenRefresh(expiresAt, response.token);
_this.trigger('sessionDataUpdated', data);

resolve(response);
Expand Down
6 changes: 3 additions & 3 deletions tests/unit/authenticators/jwt-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,7 @@ test('#restore schedules a token refresh when `refreshAccessTokens` is true.', f
data[jwt.tokenPropertyName] = token;
data[jwt.tokenExpireName] = expiresAt;

// TODO: find out of there is another way besides setting Ember.testing.
// TODO: find out if there is another way besides setting Ember.testing.
Ember.testing = false;

Ember.run(function() {
Expand Down Expand Up @@ -506,8 +506,8 @@ test('#refreshAccessToken triggers the `sessionDataUpdated` event on successful
App.authenticator.refreshAccessToken(token);

App.authenticator.one('sessionDataUpdated', function(data) {
ok(data.expiresAt, 'Verify expiresAt was added to response');
ok(data.expiresAt > (new Date()).getTime(), 'Verify is greater than now');
ok(data[jwt.tokenExpireName], 'Verify expiresAt was added to response');
ok(data[jwt.tokenExpireName] > (new Date()).getTime(), 'Verify is greater than now');
deepEqual(data.token, token);
});
});
Expand Down

0 comments on commit 4b55b11

Please sign in to comment.