From 39c32e7c5f83e30de5c2fca470f852282826b6c8 Mon Sep 17 00:00:00 2001 From: Ben Wheeler Date: Wed, 27 Nov 2019 10:40:04 -0500 Subject: [PATCH] Add lib/session.js, add refreshSessionWithRetry() --- src/lib/session.js | 29 +++++++++++++++++++++++++++ src/redux/navigation.js | 5 +++-- src/redux/session.js | 44 +++++++++++++++++++++++++++++++++++++++++ 3 files changed, 76 insertions(+), 2 deletions(-) create mode 100644 src/lib/session.js diff --git a/src/lib/session.js b/src/lib/session.js new file mode 100644 index 000000000..849cd9c68 --- /dev/null +++ b/src/lib/session.js @@ -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); + }); +}; diff --git a/src/redux/navigation.js b/src/redux/navigation.js index 8191ce5d6..5c2c37623 100644 --- a/src/redux/navigation.js +++ b/src/redux/navigation.js @@ -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)) + ); } }); diff --git a/src/redux/session.js b/src/redux/session.js index d5701cb1d..af209363e 100644 --- a/src/redux/session.js +++ b/src/redux/session.js @@ -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)); + }); +});