Keystone-classic: How to update user password

Created on 25 Mar 2015  路  11Comments  路  Source: keystonejs/keystone-classic

I'm using User model from Keystone and I don't know how to update User.password because it's encrypted.

Most helpful comment

@quynguyen13 ... the password field adds a pre save hook handler to the schema that encrypts the password for you. pre hooks don't get invoked when using Model#update, but they do when using Model#save.

Try something like this:

User.model.findOne({ email: email}, function(findError, user) {
  if (findError) {
    // handle error
  } else {
    user.password = newPassword;
    user.save(function(saveError) {
      if (saveError) {
        // handle error
      }
    });
  }
});

All 11 comments

you must share your user model and any update files in your updates/ directory

I'm not sure what @morenoh149 meant, but you can just update the password field by setting a new password and when the model is saved, it will be crypted.

@ttsirkia Help me please ! I don't know how to set a new password.
I use this code to update user password, if newPassword is a normal string, when the model is saved, the password is set directly.
Users.model.update({ email: email},
{ $set: { password: newPassword }}, function(err, result){

});

@quynguyen13 ... the password field adds a pre save hook handler to the schema that encrypts the password for you. pre hooks don't get invoked when using Model#update, but they do when using Model#save.

Try something like this:

User.model.findOne({ email: email}, function(findError, user) {
  if (findError) {
    // handle error
  } else {
    user.password = newPassword;
    user.save(function(saveError) {
      if (saveError) {
        // handle error
      }
    });
  }
});

@JohnnyEstilles Thank you!

@JohnnyEstilles Slightly off topic but is there an example floating around of updating an existing document using the Keystone update handler?

@webteckie might be able to share one

@morenoh149 hmm...is difficult to tell whether @hatzipanis is referring to the update handler that's used for CRUDs or the updates handler that's used to seed/update a database. @morenoh149 I'm assuming you think he's referring to the latter...but I'm not so sure.

@webteckie The first actually! It's probably quite simple but I'm still getting my head around all of this (and sorry to hijack this issue with something unrelated).

I know that for a new document we can use it like this:

var newNotice = new Notice.model({
    author: req.user,
});

var updater = newNotice.getUpdateHandler(req);

But how do we use it for an existing document? I've tried a few things but don't seem to be getting what I need.

@hatzipanis you may want to check #837 to see if the recommended solution helps (basically check the update handler usage in the SydJS site @ https://github.com/JedWatson/sydjs-site). Else I'll defer for someone else more knowledgeable with the update handler to lend you a hand.

@webteckie this is perfect. I looked at the SydJS but not at views/me. Tested and working perfectly in my situation. Thanks so much for your help.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

Twansparant picture Twansparant  路  5Comments

zhdan88vadim picture zhdan88vadim  路  5Comments

kamontat picture kamontat  路  5Comments

calebmcelroy picture calebmcelroy  路  3Comments

sarmadsangi picture sarmadsangi  路  5Comments