重置节点js中的密码 [英] Reset password in node js
问题描述
我正在尝试添加重置密码功能.用户输入用户名,电子邮件和全名,当这些值与数据库中的数据匹配时,密码被更改,否则显示错误. 这就是我在做什么-
Im trying to add reset password feature.User enters username,email and full name,when these values match data from database ,password is changed,otherwise an error is shown. This is what im doing-
app.post("/reset",function(req,res){
User.findByUsername(req.body.username).then(function(sanitizedUser){
if (sanitizedUser){
sanitizedUser.setPassword(req.body.password, function(){
sanitizedUser.save();
req.flash("success","password resetted");
res.redirect("/login");
});
} else {
req.flash("error","User doesnt exist");
res.redirect("/reset");
}
},function(err){
console.log(err);res.redirect("/");
});
});
但是我不仅想比较用户名,还想比较用户的电子邮件和用户名.当我添加-
User.find({username:req.body.username},{email:req.body.email},{name:req.body.name})
,然后输入一些错误的数据,页面只会不断地重新加载,而不显示错误.
我应该对此进行哪些更改?请帮忙.
But i want to compare more than just the username,i want to compare email and name of the user too.And when i add-
User.find({username:req.body.username},{email:req.body.email},{name:req.body.name})
and enter some wrong data,the page just keeps reloading rather than showing an error.
What changes should i make to do that?Please help.
我正在使用express,nodejs,mongodb
Im using express,nodejs,mongodb
推荐答案
您有一个重定向循环.
对于!sanitizedUser
,您将重定向到同一页面res.redirect('/reset')
.
In the case of !sanitizedUser
you redirect to the same page res.redirect('/reset')
.
这导致您的页面不断重新加载.
This is causing your page to keep reloading.
您应该更改以下行:
res.redirect('/reset')
到
res.status(500).send('Some useful error message')
这篇关于重置节点js中的密码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!