动态失败使用passport.js重定向

Fez*_*sta 4 node.js express passport.js

这是我的登录功能atm:

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

我需要将req.body.url放在failureRedirect网址中,因此它看起来应该像这样:

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

它不起作用,因为req变量仅在...的回调内部初始化post。我该怎么办?

小智 5

您可以使用“ 自定义回调”来动态生成回调URL,因为req对象在其中可用。