I have a user registration system that works well, however, when comparing the hash of the database with the one entered in an html field, it throws me the following error
Error: data and hash arguments required
I am sending 3 parameters just as it says in the documentation, which is the plain text password, the hash and a callback. Thanks in advance
'use strict';
const bcrypt = require('bcrypt')
module.exports = (sequelize, DataTypes) => {
const User = sequelize.define('User', {
email: {
allowNull: false,
type: DataTypes.STRING,
unique: true
},
password_hash: DataTypes.STRING,
password: DataTypes.VIRTUAL
}, {});
User.login = (email, password) => {
return User.findOne({
where: {
email: email
}
}).then(user => {
if(!user) return null
return user.authenticatePassword(password).then(valid => valid ? user : null)
})
}
/* =============== */
/* METODO PARA COMPARAR HASH */
/* =============== */
User.prototype.authenticatePassword = (password) => {
return new Promise((res, rej) => {
bcrypt.compare(password, this.password_hash, (err, valid) => {
if(err) return rej(err)
res(valid)
})
})
}
User.associate = function(models) {
// associations can be defined here
};
User.beforeCreate((user, options) => {
return new Promise((res, rej) => {
if(user.password) {
bcrypt.hash(user.password, 10, (err, hash) => {
user.password_hash = hash
res()
})
}
})
})
return User;
};
When using the traditional syntax to declare functions using
function
, the arrow function that is added as a callback when instantiating the promise will use the this value of the execution context on which it is declared, which in this case is the authenticatePassword method.Here I would take User as a reference