【发布时间】:2021-07-28 15:26:58
【问题描述】:
app.post("/login", passport.authenticate("local",), function (req, res) {
const user = new Model({
username: req.body.username,
password: req.body.password,
});
req.login(user, function (err) {
if (err) {
console.log("wrong password");
} else {
passport.authenticate("local")(req, res, function () {
res.redirect("/admin");
});
}
});
});
app.post("/admin", function (req, res) {
Model.findOne({username: "siddharth"}).exec(function(err, foundList){
if(foundList)
{
const list = new linkModel({
linkTitle: req.body.linkTitle,
linkUrl: req.body.linkUrl,
});
foundList.links.push(list);
foundList.save();
res.redirect("/admin");
}
else
{res.send("redirect to home page and then login");
res.redirect("/login");
}
});
});
当从登录路由到其他路由(管理员)进行身份验证时,我如何将用户名传递给定义了 mongoose 查询的 findone? 正如我已经明确定义的那样。
或者简单来说,我如何在路由之间传递数据?
【问题讨论】:
标签: node.js mongodb express session mongoose