Redirected to custom path on successful signup (#12372)

refs #12366 

This implements redirection based on the settings for successful member sign up!
- Removes support for redirecting to `req.path` afterwards, this was never used and
  we now have a more configurable implementation.
- Retains redirection to the homepage for unsuccessful sign up (invalid/expired token)
This commit is contained in:
Fabien 'egg' O'Carroll 2020-11-19 09:58:32 +00:00 committed by GitHub
parent 874403d3ae
commit 9ba3e96790
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -132,20 +132,38 @@ const createSessionFromMagicLink = async function (req, res, next) {
}
});
// We need to include the subdirectory,
// members is already removed from the path by express because it's a mount path
let redirectPath = `${urlUtils.getSubdir()}${req.path}`;
try {
await membersService.ssr.exchangeTokenForSession(req, res);
const member = await membersService.ssr.exchangeTokenForSession(req, res);
const subscriptions = member && member.stripe && member.stripe.subscriptions || [];
let redirectPath = '/';
const action = req.query.action || req.query['portal-action'];
if (action === 'signup') {
if (subscriptions.find(sub => ['active', 'trialing'].includes(sub.status))) {
redirectPath = settingsCache.get('members_paid_signup_redirect') || '/';
} else {
redirectPath = settingsCache.get('members_free_signup_redirect') || '/';
}
if (!redirectPath.startsWith('/')) {
redirectPath = '/' + redirectPath;
}
if (!redirectPath.endsWith('/')) {
redirectPath = redirectPath + '/';
}
}
// Do a standard 302 redirect, with success=true
searchParams.set('success', true);
res.redirect(`${urlUtils.getSubdir()}${redirectPath}?${searchParams.toString()}`);
} catch (err) {
logging.warn(err.message);
const redirectPath = '/';
// Do a standard 302 redirect to the homepage, with success=false
searchParams.set('success', false);
} finally {
res.redirect(`${redirectPath}?${searchParams.toString()}`);
res.redirect(`${urlUtils.getSubdir()}${redirectPath}?${searchParams.toString()}`);
}
};