Dynamic failureRedirect with passport.js

Issue

This is my login function atm:

app.post("/login", passport.authenticate("local", {
    failureRedirect: "/login?error=1"
}), function (req, res) {
    res.redirect(req.body.url || "/");
});

I need to put the req.body.url inside the failureRedirect url, so it should looks like:

app.post("/login", passport.authenticate("local", {
    failureRedirect: "/login?error=1&url=" + (req.body.url || "/")
}), function (req, res) {
    res.redirect(req.body.url || "/");
});

It can’t work because the req variable is inited only inside the callback of post… how can I do?

Solution

You can use Custom Callbacks to dynamically generate callbacks urls, as the req object is available inside them.

Answered By – matteospampani

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published