Add lib/session.js, add refreshSessionWithRetry()

This commit is contained in:
Ben Wheeler 2019-11-27 10:40:04 -05:00
parent 628218571b
commit 39c32e7c5f
3 changed files with 76 additions and 2 deletions

29
src/lib/session.js Normal file
View 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);
});
};

View file

@ -110,8 +110,9 @@ module.exports.handleCompleteRegistration = createProject => (dispatch => {
// to be logged in before we try creating a project due to replication lag. // to be logged in before we try creating a project due to replication lag.
window.location = '/'; window.location = '/';
} else { } else {
dispatch(sessionActions.refreshSession()); dispatch(sessionActions.refreshSessionWithRetry()).then(
dispatch(module.exports.setRegistrationOpen(false)); dispatch(module.exports.setRegistrationOpen(false))
);
} }
}); });

View file

@ -2,6 +2,7 @@ const keyMirror = require('keymirror');
const defaults = require('lodash.defaults'); const defaults = require('lodash.defaults');
const api = require('../lib/api'); const api = require('../lib/api');
const sessionLib = require('../lib/session');
const messageCountActions = require('./message-count.js'); const messageCountActions = require('./message-count.js');
const permissionsActions = require('./permissions.js'); const permissionsActions = require('./permissions.js');
@ -103,3 +104,46 @@ module.exports.refreshSession = () => (dispatch => {
return; 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));
});
});