简体   繁体   English

如何使用猫鼬更新现有用户的密码?

[英]How to update password for a existing user with mongoose?

I'm creating a feature for my website to enable users to reset there password if they forget it.我正在为我的网站创建一个功能,使用户可以在忘记密码时重置密码。

packages i'm using... Express Js (framework)我正在使用的软件包... Express Js(框架)
passport-local--mongoose,护照-本地-猫鼬,
passport-local,护照本地,
passport,护照,

i'm using passport method .setPassword in user model but password is not changing我在用户模型中使用通行证方法 .setPassword 但密码没有改变

router.post('/login/:userID/reset_password',(req, res)=>{
   const NewPass=req.body.password,
         confirm_password=req.body.confirm_password;
         console.log(NewPass);
         if(NewPass==confirm_password){
             console.log(NewPass);
             User.findById(req.params.userID, (err, user)=>{
                 if(err) console.log(err);
                 else
                   {
                       user.setPassword(NewPass,(err, user)=>{
                        if(err) console.log('set pass error'+err);
                        else{
                            console.log('new pass set successfully!!');
                            res.redirect('/login');
                        }

                       });
                   } 
             });

         }
});

I got it... I just have to write user.save(); 我明白了...我只需要写user.save(); after updating passport 护照更新后

Compare new and previous passport and then you can update password directly to database if you are using bcrypt then when you compare password you need to compare hash and if comparison is true then you can store new hash to db . 比较新护照和旧护照,如果使用bcrypt,则可以直接将密码更新到数据库,然后在比较密码时需要比较哈希,如果比较为true,则可以将新哈希存储到db。 Thanks 谢谢

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM