[英]passport.authenticate() is always executing failureRedirect after saving the user information in mongoDB
I'm a beginner to nodejs and tying to apply authentication using passport, passport-local-mongoose.我是 nodejs 的初学者,想使用护照、本地猫鼬应用身份验证。 I have a register page where user can enter mail id and password and click on register button.
我有一个注册页面,用户可以在其中输入邮件 ID 和密码,然后单击注册按钮。 when user makes a post request by clicking on that button, I want to store the mailid and hash(generated using User.register method from lpassport-local-mongoose) in mongoDB. I'm doing that using passportlocal-mongoose and then wanted to authenticate the user if there are no errors in creating the user.
当用户通过单击该按钮发出发布请求时,我想在 mongoDB 中存储 mailid 和哈希(使用 lpassport-local-mongoose 的 User.register 方法生成)。我正在使用 passportlocal-mongoose 进行此操作,然后想如果在创建用户时没有错误,则对用户进行身份验证。
app.post("/register", function(req, res){ const username = req.body.mailbox; User.register({username: username}, req.body.passwordbox, function(err, user){ if(err){ console.log(err); } else{ passport.authenticate("local", successRedirect: "/secrets", failureRedirect: "/register")(req, res); } }) });
Based on your provided context, you can try this code:根据您提供的上下文,您可以尝试以下代码:
app.post("/register", function(req,res){
const username = req.body.mailid
User.register({username:username}, req.body.password, function(err, user){
if(!err){
passport.authenticate("local")(req, res, function(){
res.redirect("/secrets");
});
}
else {
res.redirect("/register");
}
});
});
following code using the passport.authenticate() method to authenticate the user, and providing a callback function to redirect to the secrets page if authentication is successful.以下代码使用passport.authenticate()方法对用户进行身份验证,并在身份验证成功时提供回调 function 以重定向到机密页面。 If an error occurs during user creation, we redirect to the registration page.
如果在用户创建过程中发生错误,我们将重定向到注册页面。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.