Here is a get route and I have stored the previous link where it was redirected from in the req.session object under returnTo. After redirecting it hits the /login and we can see the object in the console.
router.get('/login', (req, res) => {
console.log('get in user route..', req.session)
res.render('users/login')
})
req.session :
router.post('/login', passport.authenticate('local', { failureFlash: true, failureRedirect: '/login' }), (req, res) => {
req.flash('success', 'Welcome back!')
const redirectUrl = req.session.returnTo || '/campgrounds'
res.redirect(redirectUrl)
})
But after submitting the login details it always goes to the /campgrounds route.
I have found answer after referring to the new passport.js version. In the newer version the session id is updated. So I no longer have access to it. So, what I did is I declared a variable
let ran = ''
and stored res.locals.returnTo in ran variable.
res.locals.returnTo = req.session.returnTo
ran = req.session.returnTo
res.render('users/login')
So, we have access to the retrun to url even if the seesion id is updated.