gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-03 22:10:07 25 4
gpt4 key购买 nike

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

“未处理的拒绝类型错误: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

最佳答案

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

他们现在遵循更加基于类的方法,来自 Docs 的示例了解如何完成

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

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

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

关于node.js - 未处理的拒绝 TypeError : dbUser. validPassword 不是函数 Sequelize Node React Passport,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44909738/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com