I'm using node-bcrypt along with PostgreSQL (via Sequelizejs to hash and save passwords.
The user's password is hashed in a beforeValidate hook like so:
beforeValidate: function(user, model, cb) {
bcrypt.hash(user.password, 10, function(err, hash) {
if ( err ) { throw err; }
user.password = hash;
cb(null, user);
});
}
The column on the User model in which the hash is stored is defined as:
password: { type: DataTypes.STRING, allowNull: false }
When a user is logging in (I'm using Passport for authentication), the function looks like this:
passport.use(new LocalStrategy(function(username, password, done) {
models.User.find({ username: username }).then(function(retrievedUser) {
if ( !_.isEmpty(retrievedUser) ) {
retrievedUser.verifyPassword(password, function(err, result) {
if ( err || !result ) {
return done(null, false, { message: 'Incorrect password.' });
} else {
return done(null, retrievedUser);
}
});
} else {
return done(null, false, { message: 'User could not be found at that username.' });
}
}).catch(function(err) {
return done(err);
});
}));
Which retrieves the user correctly.
And the comparison, defined here on the User model:
instanceMethods: {
verifyPassword: function(password, cb) {
bcrypt.compare(password, this.password, cb);
}
}
The verification passes just fine if the password only contains letters and/or symbols. However, any passwords with numbers never pass the comparison. Any ideas?