From 67e3fb64981fe2310b17515e1f18c32021a5e983 Mon Sep 17 00:00:00 2001 From: Julian Lam Date: Fri, 5 Feb 2021 11:05:21 -0500 Subject: [PATCH] fix: register returnTo logic to match login route Login route saves the previous page by checking for the X-Return-To header. This header is automatically set by ajaxify. Login takes this value and saves it to `req.session`. Up until now, `/register` saved the previous URL in a hidden input, and redirected based on that value, but it occasionally conflicted with req.session.returnTo. It was also confusing because it did not match how login handled the values. This commit updates the route handling so it works identically to `/login`. --- public/src/client/register.js | 7 +++---- src/controllers/authentication.js | 11 ++++------- src/controllers/index.js | 5 +++++ 3 files changed, 12 insertions(+), 11 deletions(-) diff --git a/public/src/client/register.js b/public/src/client/register.js index e05b7cc828..400ab9a870 100644 --- a/public/src/client/register.js +++ b/public/src/client/register.js @@ -17,7 +17,6 @@ define('forum/register', [ handleLanguageOverride(); - $('#referrer').val(app.previousUrl); $('#content #noscript').val('false'); email.on('blur', function () { @@ -89,10 +88,10 @@ define('forum/register', [ if (!data) { return; } - if (data.referrer) { - var pathname = utils.urlToLocation(data.referrer).pathname; + if (data.next) { + var pathname = utils.urlToLocation(data.next).pathname; - var params = utils.params({ url: data.referrer }); + var params = utils.params({ url: data.next }); params.registered = true; var qs = decodeURIComponent($.param(params)); diff --git a/src/controllers/authentication.js b/src/controllers/authentication.js index 5c16fc4022..d610aab4ce 100644 --- a/src/controllers/authentication.js +++ b/src/controllers/authentication.js @@ -34,14 +34,11 @@ async function registerAndLoginUser(req, res, userData) { userData.register = true; req.session.registration = userData; - if (req.body.referrer) { - req.session.returnTo = req.body.referrer; - } if (req.body.noscript === 'true') { res.redirect(nconf.get('relative_path') + '/register/complete'); return; } - res.json({ referrer: nconf.get('relative_path') + '/register/complete' }); + res.json({ next: nconf.get('relative_path') + '/register/complete' }); return; } const queue = await user.shouldQueueUser(req.ip); @@ -60,9 +57,9 @@ async function registerAndLoginUser(req, res, userData) { await user.joinGroupsFromInvitation(uid, userData.email); } await user.deleteInvitationKey(userData.email); - const referrer = req.body.referrer || req.session.returnTo || nconf.get('relative_path') + '/'; - const complete = await plugins.hooks.fire('filter:register.complete', { uid: uid, referrer: referrer }); - req.session.returnTo = complete.referrer; + const next = req.session.returnTo || nconf.get('relative_path') + '/'; + const complete = await plugins.hooks.fire('filter:register.complete', { uid: uid, next: next }); + req.session.returnTo = complete.next; return complete; } diff --git a/src/controllers/index.js b/src/controllers/index.js index a53c48a627..f877820712 100644 --- a/src/controllers/index.js +++ b/src/controllers/index.js @@ -144,6 +144,7 @@ Controllers.register = async function (req, res, next) { } let errorText; + const returnTo = (req.headers['x-return-to'] || '').replace(nconf.get('base_url') + nconf.get('relative_path'), ''); if (req.query.error === 'csrf-invalid') { errorText = '[[error:csrf-invalid]]'; } @@ -158,6 +159,10 @@ Controllers.register = async function (req, res, next) { } } + if (returnTo) { + req.session.returnTo = returnTo; + } + const loginStrategies = require('../routes/authentication').getLoginStrategies(); res.render('register', { 'register_window:spansize': loginStrategies.length ? 'col-md-6' : 'col-md-12',