mirror of
https://github.com/scratchfoundation/scratch-www.git
synced 2024-11-26 17:16:11 -05:00
Add lib/session.js, add refreshSessionWithRetry()
This commit is contained in:
parent
628218571b
commit
39c32e7c5f
3 changed files with 76 additions and 2 deletions
29
src/lib/session.js
Normal file
29
src/lib/session.js
Normal file
|
@ -0,0 +1,29 @@
|
|||
const api = require('./api');
|
||||
|
||||
module.exports = {};
|
||||
|
||||
module.exports.requestSessionWithRetry = (count, resolve, reject) => {
|
||||
console.log('starting refreshSessionWithRetry');
|
||||
api({
|
||||
host: '',
|
||||
uri: '/session/'
|
||||
}, (err, body, response) => {
|
||||
if (err || (response && response.statusCode === 404)) {
|
||||
console.log('refreshSessionWithRetry: resolving with session err');
|
||||
return reject(err);
|
||||
}
|
||||
if (typeof body === 'undefined' || !body.user) {
|
||||
// Retry after 500ms, 1.5s, 3.5s, 7.5s and then stop.
|
||||
if (count > 4) {
|
||||
console.log('refreshSessionWithRetry: too many tries, resolving');
|
||||
return resolve(body);
|
||||
}
|
||||
console.log(`with count ${count}, waiting ${(250 * Math.pow(2, count))} ms`);
|
||||
return setTimeout(module.exports.requestSessionWithRetry.bind(null, count + 1, resolve, reject),
|
||||
250 * Math.pow(2, count));
|
||||
}
|
||||
console.log('refreshSessionWithRetry: session found! resolving with body:');
|
||||
// console.log(body);
|
||||
return resolve(body);
|
||||
});
|
||||
};
|
|
@ -110,8 +110,9 @@ module.exports.handleCompleteRegistration = createProject => (dispatch => {
|
|||
// to be logged in before we try creating a project due to replication lag.
|
||||
window.location = '/';
|
||||
} else {
|
||||
dispatch(sessionActions.refreshSession());
|
||||
dispatch(module.exports.setRegistrationOpen(false));
|
||||
dispatch(sessionActions.refreshSessionWithRetry()).then(
|
||||
dispatch(module.exports.setRegistrationOpen(false))
|
||||
);
|
||||
}
|
||||
});
|
||||
|
||||
|
|
|
@ -2,6 +2,7 @@ const keyMirror = require('keymirror');
|
|||
const defaults = require('lodash.defaults');
|
||||
|
||||
const api = require('../lib/api');
|
||||
const sessionLib = require('../lib/session');
|
||||
const messageCountActions = require('./message-count.js');
|
||||
const permissionsActions = require('./permissions.js');
|
||||
|
||||
|
@ -103,3 +104,46 @@ module.exports.refreshSession = () => (dispatch => {
|
|||
return;
|
||||
});
|
||||
});
|
||||
|
||||
module.exports.refreshSessionWithRetry = () => (dispatch => {
|
||||
dispatch(module.exports.setStatus(module.exports.Status.FETCHING));
|
||||
return new Promise((resolve, reject) => (
|
||||
sessionLib.requestSessionWithRetry(1, resolve, reject)
|
||||
)).then(body => {
|
||||
if (typeof body === 'undefined') return dispatch(module.exports.setSessionError('No session content'));
|
||||
if (
|
||||
body.user &&
|
||||
body.user.banned &&
|
||||
banWhitelistPaths.indexOf(window.location.pathname) === -1
|
||||
) {
|
||||
window.location = '/accounts/banned-response/';
|
||||
return;
|
||||
} else if (
|
||||
body.flags &&
|
||||
body.flags.must_complete_registration &&
|
||||
window.location.pathname !== '/classes/complete_registration'
|
||||
) {
|
||||
window.location = '/classes/complete_registration';
|
||||
return;
|
||||
} else if (
|
||||
body.flags &&
|
||||
body.flags.must_reset_password &&
|
||||
!body.flags.must_complete_registration &&
|
||||
window.location.pathname !== '/classes/student_password_reset/'
|
||||
) {
|
||||
window.location = '/classes/student_password_reset/';
|
||||
return;
|
||||
}
|
||||
dispatch(module.exports.setSession(body));
|
||||
dispatch(module.exports.setStatus(module.exports.Status.FETCHED));
|
||||
|
||||
// get the permissions from the updated session
|
||||
dispatch(permissionsActions.storePermissions(body.permissions));
|
||||
if (typeof body.user !== 'undefined') {
|
||||
dispatch(messageCountActions.getCount(body.user.username));
|
||||
}
|
||||
return;
|
||||
}, err => {
|
||||
dispatch(module.exports.setSessionError(err));
|
||||
});
|
||||
});
|
||||
|
|
Loading…
Reference in a new issue