title: How to set automatic password hashing in Sequelize's user model?
body: <p>Right now I'm hashing the password on the route function and providing the hash when creating the user but I know there's a way to have this be handled through Sequelize itself. I have searched around but every answer seems to be outdated or the methods never got called. Here is my config:</p>
  
  <p><strong>server/models/User.js</strong></p>
  
  <pre><code>module.exports = (sequelize, type) =&gt; {
    const User = sequelize.define(
      "User",
      {
        user_id: {
          type: type.INTEGER,
          allowNull: false,
          primaryKey: true,
          autoIncrement: true
        },
        name: {
          type: type.STRING,
          allowNull: false
        },
        email: {
          type: type.STRING,
          allowNull: false
        },
        password: {
          type: type.STRING,
          allowNull: false
        },
        reg_date: {
          type: type.DATEONLY,
          allowNull: false,
          defaultValue: sequelize.fn("now")
        }
      },
      {
        timestamps: false
      }
    );
    return User;
  };
  </code></pre>
  
  <p><strong>server/config/sequelize.js</strong></p>
  
  <pre><code>const Sequelize = require("sequelize");
  
  const sequelize = new Sequelize(process.env.CLEARDB_DATABASE_URL);
  
  sequelize
    .authenticate()
    .then(() =&gt; {
      console.log("Connection has been established successfully.");
    })
    .catch(err =&gt; {
      console.error("Unable to connect to the database:", err);
    });
  
  const UserModel = require("../models/User");
  
  
  const User = UserModel(sequelize, Sequelize);
  
  
  module.exports = User;
  
  </code></pre>
  
  <p>This is how I'm handling hashing right now:</p>
  
  <p><strong>server/routes/register.js</strong></p>
  
  <pre><code>User.findOne({ where: { email: email } }).then(user =&gt; {
          if (!user) {
            bcrypt.hash(password, 10, (err, hash) =&gt; {
              if (err) throw err;
  
              User.create({
                name: req.body.name,
                email: email,
                password: hash
              })
                .then(user =&gt; {
                  return user;
                })
                .catch(err =&gt; console.log(err));
            });
          }
        });
  </code></pre>