未处理的拒绝TypeError:dbUser.validPassword不是函数Sequelize Node React Passport

时间:2017-07-04 15:29:56

标签: node.js reactjs express passport.js

我正在尝试在我的网络应用上使用Passport身份验证。我正在使用Sequelize ORM,Reactjs前端和快递以及节点后端。现在,当我注册用户时一切正常。当我尝试登录时出现问题。我看到用户查询数据库以找到具有正确电子邮件的用户,但是当需要比较密码时,我发现错误。

"未处理的拒绝TypeError:dbUser.validPassword不是函数"

这是我的config / passport.js文件:

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;

这是我的用户模型:

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

我还包括错误的图像。 Error Message

1 个答案:

答案 0 :(得分:1)

从续集版本> 4开始,它改变了实例方法的定义方式。

他们现在采用更基于类的方法, 来自Docs的样本,了解如何完成

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

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

您使用的语法对应于sequelize< 4。