Colt / YelpCamp

581 stars 561 forks source link

ReturnTo behavior not working #28

Open BelalKassem opened 2 years ago

BelalKassem commented 2 years ago

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. login get route login post route `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); });`

codingbeeast commented 2 years ago

Did you figure out solution to this yet?

TalibIbrahim commented 1 year ago

Having the same problem

1WINgFIRE1 commented 1 month ago

Did anyone got the solution for this?