mirror of
https://github.com/scratchfoundation/scratch-www.git
synced 2024-11-27 01:25:52 -05:00
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:
parent
a497fed338
commit
e44e958fe0
3 changed files with 27 additions and 8 deletions
|
@ -1,4 +1,5 @@
|
|||
var cookie = require('cookie');
|
||||
var defaults = require('lodash.defaults');
|
||||
var xhr = require('xhr');
|
||||
var pako = require('pako');
|
||||
|
||||
|
@ -69,11 +70,13 @@ var Jar = {
|
|||
});
|
||||
});
|
||||
},
|
||||
set: function (name, value) {
|
||||
var obj = cookie.serialize(name, value);
|
||||
var expires = '; expires=' + new Date(new Date().setYear(new Date().getFullYear() + 1)).toUTCString();
|
||||
var path = '; path=/';
|
||||
document.cookie = obj + expires + path;
|
||||
set: function (name, value, opts) {
|
||||
defaults(opts, {
|
||||
expires: new Date(new Date().setYear(new Date().getFullYear() + 1)),
|
||||
path: '/'
|
||||
});
|
||||
var obj = cookie.serialize(name, value, opts);
|
||||
document.cookie = obj;
|
||||
},
|
||||
getUnsignedValue: function (cookieName, signedValue, callback) {
|
||||
// Get a value from a signed object
|
||||
|
|
|
@ -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 () {
|
||||
return function (dispatch) {
|
||||
jar.getUnsignedValue('scratchsessionsid', 'permissions', function (err, value) {
|
||||
jar.get('permissions', function (err, value) {
|
||||
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));
|
||||
});
|
||||
};
|
||||
|
|
|
@ -89,7 +89,7 @@ module.exports.refreshSession = function () {
|
|||
dispatch(module.exports.setStatus(module.exports.Status.FETCHED));
|
||||
|
||||
// get the permissions from the updated session
|
||||
dispatch(permissionsActions.getPermissions());
|
||||
dispatch(permissionsActions.storePermissions(body.permissions));
|
||||
return;
|
||||
}
|
||||
});
|
||||
|
|
Loading…
Reference in a new issue