I am using sequelize for the first time and trying to use hooks, and additionally I am just learning about promises in JS. I have two methods of implementing a function, but I am wondering if one is asynch and the other is not?
// METHOD 1
User.addHook("beforeCreate", (user) => {
user.password = bcrypt.hashSync(user.password, bcrypt.genSaltSync(8), null);
});
// METHOD 2
User.addHook("beforeCreate", (user, options) => {
return bcrypt.hashSync(user.password, bcrypt.genSaltSync(8), null)
.then(hashedPw => {
user.password = hashedPw;
});
});
Also if anyone knows what the 'options' parameter is for that would be helpful, method 2 is essentially taken from the Sequelize docs and it has the 'options' parameter but I cannot see where it is used...
EDIT
Comments made me understand the above methods aren't asynch as they use hashSync, so I made a new implementation, but this still results in the unhashed password being saved to db...
const saltRounds = 8;
User.addHook("beforeCreate", (user) => {
bcrypt.genSalt(saltRounds, function(err, salt) {
bcrypt.hash(user.password, salt, null, function(err, hash) {
user.password = hash;
});
});
});