Passport-Local Mongoose - Change password?

Gà Rù picture Gà Rù · Jul 24, 2013 · Viewed 18.8k times · Source

I use Passport-Local Mongoose to encrypt the account's password. But I don't know how to change the password.

Can you give some document or example to do it? Thank you.

Answer

user1441287 picture user1441287 · Aug 4, 2013

Looking at the source there is a function that is added to the schema called setPassword. I believe that after authenticating you can call it to change the password for the user.

schema.methods.setPassword = function (password, cb) {
    if (!password) {
        return cb(new BadRequestError(options.missingPasswordError));
    }

    var self = this;

    crypto.randomBytes(options.saltlen, function(err, buf) {
        if (err) {
            return cb(err);
        }

        var salt = buf.toString('hex');

        crypto.pbkdf2(password, salt, options.iterations, options.keylen, function(err, hashRaw) {
            if (err) {
                return cb(err);
            }

            self.set(options.hashField, new Buffer(hashRaw, 'binary').toString('hex'));
            self.set(options.saltField, salt);

            cb(null, self);
        });
    });
};