I am having trouble implementing the feature that allows you to redirect the user to where they were before they were asked to log in. In an effort to debug the code, I tried console.logging the session twice: once in the login get route (i.e. the route that renders the login page), and once in the login post route (i.e. the route the login form submits to). As seen in the screenshots below, in the login get route, the session does contain returnTo: '/campgrounds/new'. However, the session in the login post route does not. The only piece of code that gets executed between these two console.logs is the passport.authenticate() middleware. It seems like that middleware is responsible for the loss of returnTo: '/campgrounds/new' and I can't seem to figure out how to fix that.
`router.get('/login', (req, res) => {
console.log("session inside login get route:");
console.log(req.session);
res.render('users/login');
});
I am having trouble implementing the feature that allows you to redirect the user to where they were before they were asked to log in. In an effort to debug the code, I tried console.logging the session twice: once in the login get route (i.e. the route that renders the login page), and once in the login post route (i.e. the route the login form submits to). As seen in the screenshots below, in the login get route, the session does contain returnTo: '/campgrounds/new'. However, the session in the login post route does not. The only piece of code that gets executed between these two console.logs is the passport.authenticate() middleware. It seems like that middleware is responsible for the loss of returnTo: '/campgrounds/new' and I can't seem to figure out how to fix that. `router.get('/login', (req, res) => { console.log("session inside login get route:"); console.log(req.session); res.render('users/login'); });
router.post('/login', passport.authenticate('local', { failureFlash: true, failureRedirect: '/login' }), (req, res) => { console.log("session inside login post route:"); console.log(req.session); req.flash('success', 'welcome back!'); const redirectUrl = req.session.returnTo || '/campgrounds'; delete req.session.returnTo; res.redirect(redirectUrl); });`