Express- Redirecting to previous page

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

Solution

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 router.post("/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); });