Marcus Daniel
Marcus Daniel

Reputation: 103

Ignoring password ' ' on update with Sequelize

I need help, I need to check if the password is "EMPTY" in the update clause. If empty, it should update, otherwise not.

import bcrypt from 'bcrypt-nodejs';

module.exports = function (sequelize, DataTypes) {
  const Users = sequelize.define('users', {
    id: {
      type: DataTypes.INTEGER,
      primaryKey: true,
      unique: true,
      autoIncrement: true,
      field: 'id_user_acess'
    },
    username: {
      type: DataTypes.STRING,
      allowNull: true
    },
    nome: {
      type: DataTypes.STRING,
      allowNull: true
    },
    password: {
      type: DataTypes.STRING,
      allowNull: false
    }
  }, {
      tableName: 'tb_user_acess',
      timestamps: false,
      classMethods: {
        generateHash: function(password) {
          return bcrypt.hashSync(password, bcrypt.genSaltSync(10), null);
        },
        comparePassword: function(canditate, password) {
          console.log(canditate);
          console.log(password);
          return bcrypt.compareSync(canditate, password);
        }
      },
      hooks: {
        beforeCreate: function (model) {
          let user = model.dataValues;
          user.password = Users.generateHash(user.password);
          return user;
        },
        beforeUpdate: function (model) {
          let user = model.dataValues;
          if(user.password == ''){
            exclude: user.password;
          }else{
            user.password = Users.generateHash(user.password); 
            console.log(user.password);
          }

          return user;
        }
      }
    });
  return Users;
};

I tried to apply something but it did not have much effect This is my model with sequelize , plz some light <3

Upvotes: 0

Views: 1574

Answers (2)

Kevin N.
Kevin N.

Reputation: 173

If I understand the question, you don't want to update the user's password if field is empty. If so, just add a check and return null.

  function hashPassword(user) {
     const password = user.password || user.attributes.password;
     if (!user.changed('password')) return null;
    return bcrypt.genSaltAsync(5).then(salt =>
      bcrypt.hashAsync(password, salt, null).then((hash) => {
        user.password = hash; // eslint-disable-line
      })
    );
  }

  User.beforeCreate(hashPassword);
  User.beforeUpdate(hashPassword);

Upvotes: 3

kevgathuku
kevgathuku

Reputation: 348

I think you would need to use Sequelize Validations to ensure that the password provided is not empty. Try something like this

module.exports = function (sequelize, DataTypes) {
  const Users = sequelize.define('users', {
    id: {
      type: DataTypes.INTEGER,
      primaryKey: true,
      unique: true,
      autoIncrement: true,
      field: 'id_user_acess'
    },
    username: {
      type: DataTypes.STRING,
      allowNull: true
    },
    nome: {
      type: DataTypes.STRING,
      allowNull: true
    },
    password: {
      type: DataTypes.STRING,
      allowNull: false,
      validate: {
          notEmpty: true
      }
    }

Upvotes: 0

Related Questions