Here's what I would love to have happen: The user clicks the button from a "thanks for submitting this thing, you should also consider signing in" partial. They are taken to the site's user login page (or sign up). User logs in and is redirected. In most instances the res.redirect from that login page takes them back to where they were, which is great (and works)! Problem is in this one case I don't want that to happen, I want them to go to the home page. The tricky part: the page they are coming from in this case is a partial, so the url is for the main page (the form they submitted already) which DOES need the correct backlink because there was a "you should sign in first" button before the form, too, so setting an exception in app.js won't work. I think I need to just insert a step in the history from the button in the partial so the redirect goes there instead.
One more complication: the url isn't exactly the same so that might be hemming up push/replacestate(?). i.e., home ('/') is www.mypage.com , login is mypage.com/login , main page for this partial is mypage.com/section-of-these-forms/dataid
Here's where I'm at, with the snippet from the partial. It's going to the join or login page but when submit is hit over there to create an account it's still going back to the "master" of the partial instead of what I thought was inserting a redirecting step...
If there's a better way to do this, that would be awesome too.
<a href="/login">
<button id="btnGoHome" class="btn btn-primary">Sign In</button>
</a>
<script>
$(document).ready(function() {
// Listen for 'btnGoHome' onclick
$("btnGoHome").click(function() => {
//when the user finishes logging in or joining they will be sent home instead of back to here because here is now homepage
window.history.replaceState(null, '', '/');
});
});
</script>
The submit at the login/join point in users is:
req.flash('success', { msg: 'Success! You are logged in.' });
return res.redirect(req.session.returnTo || '/');
The redirect code at the join/login page to handle other exceptions like not redirecting to login from successful login is:
app.use(function (req, res, next) {
// After login, redirect back to the last page user was on
if (shouldRedirectPath(req)) req.session.returnTo = req.path;
next();
});