Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- module.exports = function(sequelize, DataTypes) {
- var users = sequelize.define('users', {
- id: { type: DataTypes.INTEGER, allowNull: false, autoIncrement: true, primaryKey: true },
- email: { type: DataTypes.TEXT, allowNull: false, validate: { isEmail: true } },
- username: { type: DataTypes.TEXT, allowNull: false },
- firstName: { type: DataTypes.TEXT, allowNull: false },
- lastName: { type: DataTypes.TEXT, allowNull: false },
- vat: { type: DataTypes.STRING, allowNull: true },
- userType: { type: DataTypes.INTEGER, allowNull: false },
- status: { type: DataTypes.STRING, allowNull: true },
- token: { type: DataTypes.STRING, allowNull: true },
- password: {
- type: DataTypes.STRING,
- allowNull: false,
- set: function(v) {
- var salt = bcrypt.genSaltSync(10);
- var hash = bcrypt.hashSync(v, salt);
- this.setDataValue('password', hash);
- }
- }
- }, {
- classMethods: {
- associate: function(models) {
- users.hasMany(models.leads);
- users.hasMany(models.appointments, { onDelete: 'cascade', hooks:true, foreignKey: 'userId' });
- users.belongsTo(models.category);
- users.belongsTo(models.sub_category);
- users.belongsTo(models.market_subcat);
- users.belongsTo(models.market);
- },
- validPassword: function(password, passwd, done, user){
- bcrypt.compare(password, passwd, function(err, isMatch){
- if (err) console.log(err)
- if (isMatch) {
- return done(null, user)
- } else {
- return done(null, false)
- }
- })
- }
- },
- instanceMethods: {
- comparePassword : function(candidatePassword, cb) {
- bcrypt.compare(candidatePassword, this.getDataValue('password'), function(err, isMatch) {
- if(err) return cb(err);
- cb(null, isMatch);
- });
- },
- toJSON: function () {
- var values = this.get();
- delete values.password;
- return values;
- }
- }
- });
- return users;
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement