Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unhandled rejection TypeError: dbUser.validPassword is not a function Sequelize Node React Passport

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.

"Unhandled rejection TypeError: dbUser.validPassword is not a function"

here is my config/passport.js file:

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;

Here is my User Model:

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;
}

I am also including an image of the error. Error Message

like image 437
Patrick Bentley Avatar asked May 24 '26 20:05

Patrick Bentley


1 Answers

As of sequelize version >4, it has changed the way instance methods are defined.

They follow a more class based approach now, A sample from the Docs for how it has to be done

 const Model = sequelize.define('Model', { ... });

 // Class Method 
Model.associate = function (models) { ...associate the models }; 
// Instance Method
Model.prototype.someMethod = function () {..}

The syntax you are using corresponds to sequelize < 4.

like image 108
Shivam Avatar answered May 27 '26 09:05

Shivam