2017-04-26 92 views
0

我试图添加重置密码功能。当这些值与数据库中的数据匹配时,用户输入用户名,电子邮件和全名,密码更改,否则会显示错误。 这是林doing-重置节点js中的密码

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("/"); 
}); 

}); 

但我想比较的不仅仅是用户名多了,我想比较用户too.And的电子邮件地址和名字时,我添加 - User.find({username:req.body.username},{email:req.body.email},{name:req.body.name})并输入一些错误的数据,该页面只是重新加载而不是显示错误。 我应该做些什么改变?请帮助。

IM使用快递,的NodeJS,MongoDB的

+0

你应该改变行? http://passportjs.org/ –

+0

是的,即时通讯使用Passport @ DanGreen-Leipciger –

回答

1

你有重定向循环

!sanitizedUser的情况下,您重定向到相同的页面res.redirect('/reset')

这会导致您的页面继续重新加载。您是否使用护照

res.redirect('/reset')

res.status(500).send('Some useful error message')

+0

它仍然不工作,顺便说一句你考虑到即时通讯使用多个通用搜索(如我在问题结束时提到) –

+0

它适用于当我只使用findByUsername() –

+0

你尝试过的硬编码值?即User.find({username:“dan”},{email:“[email protected]”},{name:“Dan Green-Leipciger”})? –

0
if(req.body.username && req.body.email && req.body.name && req.body.password){ 

    //add logic to hash password below 
    let hashedPassword = req.body.password; 


    Model.findOneAndUpdate(
     { username : req.body.username, email : req.body.email, name : req.body.name }, 
     { "$set" : { password : hashedPassword } }, 
     //if below option "new" is set to true, call back will return new document else it will return old document beforeupdate 
     { new : true }, 
     //call back 
     function(err, person){ 
      if(err){ 
       //err found 
       res.send(err); 
      } 
      else{ 
       // no error and also person exists 
       res.send('Password successfully updated..'); 
      } 
     } 
    ); 
} 
else{ 
    res.send('Please provide all details..'); 
} 
+0

它显示'密码成功更新..',但新密码不起作用,旧密码不起作用 –

+0

如果您在成功执行console.log(人)其他部分,您是否获得人物对象?它显示哪个密码? –

+0

它显示新的密码 –