mirror of
https://github.com/scratchfoundation/scratch-www.git
synced 2024-11-23 07:38:07 -05:00
Merge pull request #923 from LLK/hotfix/session-cookie
Remove interactions with session cookie
This commit is contained in:
commit
162aa25856
5 changed files with 27 additions and 61 deletions
|
@ -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
|
||||||
|
|
|
@ -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));
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
|
@ -4,11 +4,9 @@ var scheduleReducer = require('./conference-schedule.js').scheduleReducer;
|
||||||
var detailsReducer = require('./conference-details.js').detailsReducer;
|
var detailsReducer = require('./conference-details.js').detailsReducer;
|
||||||
var permissionsReducer = require('./permissions.js').permissionsReducer;
|
var permissionsReducer = require('./permissions.js').permissionsReducer;
|
||||||
var sessionReducer = require('./session.js').sessionReducer;
|
var sessionReducer = require('./session.js').sessionReducer;
|
||||||
var tokenReducer = require('./token.js').tokenReducer;
|
|
||||||
|
|
||||||
var appReducer = combineReducers({
|
var appReducer = combineReducers({
|
||||||
session: sessionReducer,
|
session: sessionReducer,
|
||||||
token: tokenReducer,
|
|
||||||
permissions: permissionsReducer,
|
permissions: permissionsReducer,
|
||||||
conferenceSchedule: scheduleReducer,
|
conferenceSchedule: scheduleReducer,
|
||||||
conferenceDetails: detailsReducer
|
conferenceDetails: detailsReducer
|
||||||
|
|
|
@ -3,7 +3,6 @@ var defaults = require('lodash.defaults');
|
||||||
|
|
||||||
var api = require('../lib/api');
|
var api = require('../lib/api');
|
||||||
var permissionsActions = require('./permissions.js');
|
var permissionsActions = require('./permissions.js');
|
||||||
var tokenActions = require('./token.js');
|
|
||||||
|
|
||||||
var Types = keyMirror({
|
var Types = keyMirror({
|
||||||
SET_SESSION: null,
|
SET_SESSION: null,
|
||||||
|
@ -86,12 +85,11 @@ module.exports.refreshSession = function () {
|
||||||
window.location.pathname !== '/classes/student_password_reset/') {
|
window.location.pathname !== '/classes/student_password_reset/') {
|
||||||
return window.location = '/classes/student_password_reset/';
|
return window.location = '/classes/student_password_reset/';
|
||||||
} else {
|
} else {
|
||||||
dispatch(tokenActions.getToken());
|
|
||||||
dispatch(module.exports.setSession(body));
|
dispatch(module.exports.setSession(body));
|
||||||
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;
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
|
@ -1,49 +0,0 @@
|
||||||
var keyMirror = require('keymirror');
|
|
||||||
var jar = require('../lib/jar.js');
|
|
||||||
|
|
||||||
var Types = keyMirror({
|
|
||||||
SET_TOKEN: null,
|
|
||||||
SET_TOKEN_ERROR: null,
|
|
||||||
USE_TOKEN: null
|
|
||||||
});
|
|
||||||
|
|
||||||
module.exports.tokenReducer = function (state, action) {
|
|
||||||
// Reducer for updating the api token
|
|
||||||
if (typeof state === 'undefined') {
|
|
||||||
state = '';
|
|
||||||
}
|
|
||||||
switch (action.type) {
|
|
||||||
case Types.SET_TOKEN:
|
|
||||||
return action.token;
|
|
||||||
case Types.SET_TOKEN_ERROR:
|
|
||||||
// TODO: do something with the error
|
|
||||||
return state;
|
|
||||||
default:
|
|
||||||
return state;
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
module.exports.getToken = function () {
|
|
||||||
return function (dispatch) {
|
|
||||||
jar.getUnsignedValue('scratchsessionsid', 'token', function (err, value) {
|
|
||||||
if (err) return dispatch(module.exports.setTokenError(err));
|
|
||||||
|
|
||||||
value = value || '';
|
|
||||||
return dispatch(module.exports.setToken(value));
|
|
||||||
});
|
|
||||||
};
|
|
||||||
};
|
|
||||||
|
|
||||||
module.exports.setToken = function (token) {
|
|
||||||
return {
|
|
||||||
type: Types.SET_TOKEN,
|
|
||||||
token: token
|
|
||||||
};
|
|
||||||
};
|
|
||||||
|
|
||||||
module.exports.setTokenError = function (error) {
|
|
||||||
return {
|
|
||||||
type: Types.SET_TOKEN_ERROR,
|
|
||||||
error: error
|
|
||||||
};
|
|
||||||
};
|
|
Loading…
Reference in a new issue