Skip to content

Commit

Permalink
Return stored token data in initial session.
Browse files Browse the repository at this point in the history
This keeps the page from thrashing in the UI by checking if there’s a valid JWT and returning its payload as the initial session data in the custom, internal getter.
  • Loading branch information
marshallswain committed Jan 25, 2017
1 parent 834c7cf commit 9166592
Showing 1 changed file with 8 additions and 2 deletions.
10 changes: 8 additions & 2 deletions session/session.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ var errors = require('feathers-errors');
var authAgent = require('feathers-authentication-popups').authAgent;
var decode = require('jwt-decode');
var payloadIsValid = require('../utils/utils').payloadIsValid;
var getStoredToken = require('../utils/utils').getStoredToken;
var hasValidToken = require('../utils/utils').hasValidToken;
var convertLocalAuthData = require('../utils/utils').convertLocalAuthData;
var Observation = require('can-observation');
Expand All @@ -24,6 +25,7 @@ module.exports = connect.behavior('data/feathers-session', function () {

var options = feathersClient.passport.options;
var Session = this.Map;
var tokenLocation = options.tokenKey || options.cookie;

Object.defineProperty(Session, 'current', {
get: function () {
Expand All @@ -39,7 +41,12 @@ module.exports = connect.behavior('data/feathers-session', function () {
}
});
}
return zoneStorage.getItem('can-connect-feathers-session');
var tokenData;
if (hasValidToken(tokenLocation)) {
var token = getStoredToken(tokenLocation);
tokenData = decode(token);
}
return zoneStorage.getItem('can-connect-feathers-session') || tokenData;
}
});

Expand Down Expand Up @@ -81,7 +88,6 @@ module.exports = connect.behavior('data/feathers-session', function () {
},
getData: function () {
return new Promise(function (resolve, reject) {
var tokenLocation = options.tokenKey || options.cookie;
if (hasValidToken(tokenLocation) && !window.doneSsr) {
feathersClient.authenticate()
.then(function (data) {
Expand Down

0 comments on commit 9166592

Please sign in to comment.