Express- Redirecting to previous page

Express- Redirecting to previous page after authentication in node.js


isLoggedIn = function(req, res, next){ if(req.isAuthenticated()){ return next(); } else{ req.flash("error", "You need to be logged in as a user"); res.redirect("/login"); }

use the above middleware with your router

router.get("/account", isLoggedIn, function(req, res){ }); //handling login logic"/login", passport.authenticate("local", { successReturnToOrRedirect: "/", failureRedirect: "/login" }),function(req, res){ User.findById(req.user._id); var redirectionUrl = req.session.redirectUrl || '/'; delete req.session.redirectUrl; res.redirect(redirectionUrl); });