re: Building a NodeJS Web App Using PassportJS for Authentication VIEW POST

FULL DISCUSSION
 

To get the user model to work, I changed the code into the following

module.exports = function(sequelize, DataTypes) {
var User = sequelize.define('User', {
email: {
type: DataTypes.STRING,
allowNull: false,
unique: true,
validate: {
isEmail: true
}
},
password: {
type: DataTypes.STRING,
allowNull: false
}
}, {
hooks: {
beforeCreate: function(user) {
user.password = bcrypt.hashSync(user.password, bcrypt.genSaltSync(10), null);
}
}
})

// Creating a custom method for our User model.
//This will check if an unhashed password entered by the
//user can be compared to the hashed password stored in our database
User.prototype.validPassword = function(password) {
return bcrypt.compareSync(password, this.password);
};
// Hooks are automatic methods that run during various phases of the User Model lifecycle
// In this case, before a User is created, we will automatically hash their password
/*
User.hook("beforeCreate", function(user) {
user.password = bcrypt.hashSync(user.password, bcrypt.genSaltSync(10), null);
});
*/
return User;
};

 

I am glad it worked with the update. I will try to figure out your add and incorporate it for others.

 

All long day search similar articles and debugging the code, finally, the code can run.

i changed line 27 of index.js
from
const model = sequelize'import';
db[model.name] = model;
become
const model = (path.join(__dirname, file));
sequelize'import';
db[model.name] = model;

But i still wonder why its can work.

And then I try @lawmaina78 , its work too without changing index.js

Note: I newbie, this is my second-day doing nodejs before I'm C# Programmer.

Thanks, Ricard Debrah for the awesome article.

code of conduct - report abuse