I have implemented express-session and included a middleware that assigns the value of req.session.returnTo to the originalUrl.
router.post(
'/login',
passport.authenticate('local', {
failureFlash: true,
failureRedirect: '/login',
}),
(req, res) => {
const redirectUrl = req.session.returnTo || '/home';
delete req.session.returnTo;
res.redirect(redirectUrl);
}
);
The code above is designed to redirect to the originalUrl if it exists. However, I noticed that although the url is stored in returnTo within the session, it mysteriously disappears just before this post request.
Even after removing the 'delete req.session.returnTo' line, the redirection still defaults to /home. When making a GET request to /login, the url is present, but somehow vanishes right before the POST request, resulting in the inconsistent behavior.