Move permissions storage to client-side cookie

Instead of retrieving the permission from the session cookie, store permission data from /session/, and cache it in a cookie.  On subsequent page loads, the permission information will be retrieved from the cookie more quickly than the /session/ endpoint returns. When the session changes, the cookie and permissions state is updated to reflect the new state.
This commit is contained in:
Ray Schamp 2016-09-08 12:52:22 -04:00
parent a497fed338
commit e44e958fe0
3 changed files with 27 additions and 8 deletions

View file

@ -1,4 +1,5 @@
var cookie = require('cookie'); var cookie = require('cookie');
var defaults = require('lodash.defaults');
var xhr = require('xhr'); var xhr = require('xhr');
var pako = require('pako'); var pako = require('pako');
@ -69,11 +70,13 @@ var Jar = {
}); });
}); });
}, },
set: function (name, value) { set: function (name, value, opts) {
var obj = cookie.serialize(name, value); defaults(opts, {
var expires = '; expires=' + new Date(new Date().setYear(new Date().getFullYear() + 1)).toUTCString(); expires: new Date(new Date().setYear(new Date().getFullYear() + 1)),
var path = '; path=/'; path: '/'
document.cookie = obj + expires + path; });
var obj = cookie.serialize(name, value, opts);
document.cookie = obj;
}, },
getUnsignedValue: function (cookieName, signedValue, callback) { getUnsignedValue: function (cookieName, signedValue, callback) {
// Get a value from a signed object // Get a value from a signed object

View file

@ -20,12 +20,28 @@ module.exports.permissionsReducer = function (state, action) {
} }
}; };
module.exports.storePermissions = function (permissions) {
permissions = permissions || {};
return function (dispatch) {
jar.set('permissions', permissions, {
encode: function (value) {
return encodeURIComponent(JSON.stringify(value));
}
});
return dispatch(module.exports.setPermissions(permissions));
};
};
module.exports.getPermissions = function () { module.exports.getPermissions = function () {
return function (dispatch) { return function (dispatch) {
jar.getUnsignedValue('scratchsessionsid', 'permissions', function (err, value) { jar.get('permissions', function (err, value) {
if (err) return dispatch(module.exports.setPermissionsError(err)); if (err) return dispatch(module.exports.setPermissionsError(err));
value = value || {}; try {
value = JSON.parse(decodeURIComponent(value)) || {};
} catch (e) {
value = {};
}
return dispatch(module.exports.setPermissions(value)); return dispatch(module.exports.setPermissions(value));
}); });
}; };

View file

@ -89,7 +89,7 @@ module.exports.refreshSession = function () {
dispatch(module.exports.setStatus(module.exports.Status.FETCHED)); dispatch(module.exports.setStatus(module.exports.Status.FETCHED));
// get the permissions from the updated session // get the permissions from the updated session
dispatch(permissionsActions.getPermissions()); dispatch(permissionsActions.storePermissions(body.permissions));
return; return;
} }
}); });