I am using express session and I have a middleware that sets the req.session.returnTo to the orginalUrl.
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);
}
);
This is my code that redirects to the originalUrl if there is. I logged the whole session and the url is there under returnTo but gets removed right before this post request.
I tried removing the delete req.session.returnTo and it still doesnt work. It is there when the app makes a get request to /login but gets removed right before /post and then it always redirects to /home.
Add keepSessionInfo: true
to passport.authenticate
.
router.post(
'/login',
passport.authenticate('local', {
failureFlash: true,
failureRedirect: '/login',
keepSessionInfo: true
}),
(req, res) => {
const redirectUrl = req.session.returnTo || '/home';
delete req.session.returnTo;
res.redirect(redirectUrl);
}
);