Find a way to update the user passport with passport-local-mongoose Find a way to update the user passport with passport-local-mongoose mongoose mongoose

Find a way to update the user passport with passport-local-mongoose


Assuming you've added the passport-local-mongoose plugin to your user schema, you should be able to callsetPassword(password, cb) on your user schema.

yourSchemaName.findById(id, function(err, user) {    user.setPassword(req.body.password, function(err) {        if (err) //handle error        user.save(function(err) {            if (err) //handle error            else //handle success        });    });});


If you want to change the password you can use changePassword command.here is an example

router.post('/changepassword', function(req, res) {// Search for user in databaseUser.findOne({ _id: 'your id here' },(err, user) => {  // Check if error connecting  if (err) {    res.json({ success: false, message: err }); // Return error  } else {    // Check if user was found in database    if (!user) {      res.json({ success: false, message: 'User not found' }); // Return error, user was not found in db    } else {      user.changePassword(req.body.oldpassword, req.body.newpassword, function(err) {         if(err) {                  if(err.name === 'IncorrectPasswordError'){                       res.json({ success: false, message: 'Incorrect password' }); // Return error                  }else {                      res.json({ success: false, message: 'Something went wrong!! Please try again after sometimes.' });                  }        } else {          res.json({ success: true, message: 'Your password has been changed successfully' });         }       })    }  }});});

If you want to change the password without using the old password you can use setPassword method. Here is an example

 user.setPassword(req.body.password, function(err,user){if (err) {    res.json({success: false, message: 'Password could not be saved. Please try again!'})} else {   res.json({success: true, message: 'Your new password has been saved successfully'})         }          });