From 9ba3e9679092a1ebc48c8282a9211db4ced63f5f Mon Sep 17 00:00:00 2001 From: Fabien 'egg' O'Carroll Date: Thu, 19 Nov 2020 09:58:32 +0000 Subject: [PATCH] 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) --- core/server/services/members/middleware.js | 32 +++++++++++++++++----- 1 file changed, 25 insertions(+), 7 deletions(-) diff --git a/core/server/services/members/middleware.js b/core/server/services/members/middleware.js index bebf9ef661..a60fcb5329 100644 --- a/core/server/services/members/middleware.js +++ b/core/server/services/members/middleware.js @@ -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()}`); } };