1

See the full error code below:

crypto.js:601
    return binding.PBKDF2(password, salt, iterations, keylen, callback);
                   ^
TypeError: Not a buffer
    at pbkdf2 (crypto.js:601:20)
    at Object.exports.pbkdf2Sync (crypto.js:592:10)
    at model.UserSchema.methods.hashPassword (/Users/markie13/documents/mean/meanPassport/app/models/user.server.model.js:59:16)
    at model.UserSchema.methods.authenticate (/Users/markie13/documents/mean/meanPassport/app/models/user.server.model.js:63:32)
    at Query. (/Users/markie13/documents/mean/meanPassport/config/strategies/local.js:19:14)
    at /Users/markie13/documents/mean/meanPassport/node_modules/mongoose/node_modules/kareem/index.js:177:19
    at /Users/markie13/documents/mean/meanPassport/node_modules/mongoose/node_modules/kareem/index.js:109:16
    at process._tickCallback (node.js:442:13)

I'm working the Amos Haviv's book MEAN Stack Development and am in chapter 6 - if anyone cares.

Here is the code that is throwing the error (see comments for line numbers):

UserSchema.pre('save', function (next) {
    if (this.password) {
        this.salt = new Buffer(crypto.randomBytes(16).toString('base64'), 'base64');
        this.password = this.hashPassword(this.password);
    }
    next();
});
UserSchema.methods.hashPassword = function (password) {
    return crypto.pbkdf2Sync(password, this.salt, 10000, 64).toString('base64');//line 59
};
UserSchema.methods.authenticate = function (password) {
    return this.password === this.hashPassword(password);//line 63
};
Artjom B.
  • 61,146
  • 24
  • 125
  • 222

1 Answers1

1

So, it turns out that I was populating the Mongo db with a non-hashed string for the password.

Once I logged in with a hashed password from the db, it worked fine.

Now I need to learn error checking...