title: Unhandled rejection TypeError: dbUser.validPassword is not a function Sequelize Node React Passport
body: <p>I am attempting to use Passport Authentication on my web app. I am using Sequelize ORM, Reactjs front-end and express and node back end. Right now, when I register a user everything works fine. the problem comes when I try to login. I see the user querying the DB to find the user with correct email, but when it is time to compare passwords, i am catching an error. </p>
  
  <p>"Unhandled rejection TypeError: dbUser.validPassword is not a function" </p>
  
  <p>here is my config/passport.js file:</p>
  
  <pre><code>var passport = require("passport");
  var LocalStrategy = require("passport-local").Strategy;
  var db = require("../models");
  // Telling passport we want to use a Local Strategy. In other words, we 
  want login with a username/email and password
  passport.use(new LocalStrategy(
  // Our user will sign in using an email, rather than a "username"
  {
    usernameField: "email"
   },
    function(email, password, done) {
  // When a user tries to sign in this code runs
  db.User.findOne({
    where: {
      email: email
    }
  }).then(function(dbUser) {
    // If there's no user with the given email
    if (!dbUser) {
      return done(null, false, {
        message: "Incorrect email."
      });
    }
    // If there is a user with the given email, but the password the user 
  gives us is incorrect
    else if (!dbUser.validPassword(password)) {
      return done(null, false, {
        message: "Incorrect password."
      });
    }
    // If none of the above, return the user
    return done(null, dbUser);
  });
  }
  ));
  // In order to help keep authentication state across HTTP requests,
  // Sequelize needs to serialize and deserialize the user
  // Just consider this part boilerplate needed to make it all work
  passport.serializeUser(function(user, cb) {
    cb(null, user);
  });
  passport.deserializeUser(function(obj, cb) {
  cb(null, obj);
  });
  // Exporting our configured passport
  module.exports = passport;
  </code></pre>
  
  <p>Here is my User Model:</p>
  
  <pre><code>var bcrypt = require("bcrypt-nodejs");
  
  [![enter image description here][1]][1]module.exports = function(sequelize, DataTypes){
  var User = sequelize.define("User", {
     email: {
      type: DataTypes.STRING,
      allowNull: false,
      validate: {
        isEmail: true
      }
  },
      password: {
    type: DataTypes.STRING,
    allowNull: false
  },
  
  },{
  
       classMethods: {
          associate: function(models) {
              User.hasOne(models.Educator, {
                  onDelete: "cascade"
              });
  
              User.hasOne(models.Expert, {
                  onDelete: "cascade"
              });
          }
       },
  
  
          instanceMethods: {
              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
          hooks: {
              beforeCreate: function(user, options) {
                 console.log(user, options )
                  user.password = bcrypt.hashSync(user.password, bcrypt.genSaltSync(10), null);
  
              }
          }
  
  })
  return User;
  }
  </code></pre>
  
  <p>I am also including an image of the error. 
  <a href="https://i.stack.imgur.com/vTHqt.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/vTHqt.png" alt="Error Message"></a></p>