gpt4 book ai didi

javascript - Sequelize : Before Create Hook is Not Working As Intended

转载 作者:太空宇宙 更新时间:2023-11-04 01:59:43 27 4
gpt4 key购买 nike

我正在尝试对通过 Sequelize 创建的模型执行 beforeCreate 操作。我试图在创建用户之前保存密码和盐。但是,创建的用户没有加密的密码或盐。我对 Node.JS 不太熟悉,但我认为这与其异步性质有关。知道如何正确引入回调以便我的创建函数按预期运行吗?

型号:

'use strict';
var Promise = require("bluebird");
var bcrypt =Promise.promisifyAll(require("bcrypt-nodejs"));
const SALT_ROUNDS = 10;

module.exports = function(sequelize, DataTypes) {
var User = sequelize.define('User', {
username: { type: DataTypes.STRING, unique: true, allowNull: false, validate: { notEmpty: true } },
email: { type: DataTypes.STRING, unique: true, allowNull: false, isEmail: true },
phone_number: DataTypes.STRING,
password_hash: { type: DataTypes.STRING, allowNull: false, unique: true, validate: { notEmpty: true } },
password_salt: DataTypes.STRING,
first_name: DataTypes.STRING,
last_name: DataTypes.STRING,
user_type: DataTypes.INTEGER,
two_factor_enabled: { type: DataTypes.BOOLEAN, defaultValue: false, },
email_verified: DataTypes.DATE,
active: { type: DataTypes.BOOLEAN, defaultValue: true, },
}, {
classMethods: {
associate: function(models) {
// associations can be defined here
},
validPassword: function(password, passwd, callback) {
bcrypt.compare(password, passwd, function(err, isMatch) {
if (isMatch) {
return callback(null, true);
} else {
return callback(null, false);
}
});
},
},
hooks: {
beforeCreate: function(user, {}) {
bcrypt.genSalt(SALT_ROUNDS, function(err, salt) {
bcrypt.hash(user.password_hash, salt, function(){}, function(err, hash) {
if (err) {
return sequelize.Promise.reject(err);
}

user.setDataValue('password_hash',hash);
user.setDataValue('password_salt',salt);
});

});
}
},
instanceMethods: {
generateHash: function(password) {
return bcrypt.hashSync(password, bcrypt.genSaltSync(10), null);
},
validPassword: function(password) {
return bcrypt.compareSync(password, this.password);
}
}
});

//User.associate = (models) => {
// User.hasMany(models.UserType, {
// foreignKey: 'userId',
// as: 'userTypes'
// });
//};

return User;
};

电话如下:

return db.User
.create({
first_name: req.body.first_name,
last_name: req.body.last_name,
username: req.body.username,
email: req.body.email,
password_hash: req.body.password
})
.then(user => res.status(201).send(user))
.catch(error => res.status(400).send(error));
},

最佳答案

我个人使用包含在node.js中的加密:https://nodejs.org/api/crypto.html

我有一个创建哈希的函数:

function createHash(password, salt) {
const generatedSalt = typeof salt !== 'undefined' ? salt : crypto.randomBytes(128).toString('base64');
const hmac = crypto.createHmac('sha256', generatedSalt);
hmac.update(password);
const hashedPassword = hmac.digest('hex');
return {
salt: generatedSalt,
hash: hashedPassword
};
}

在我的用户模型中:

beforeCreate: (user, options, cb) => {
const saltAndHash = createHash(user.password);
user.salt = saltAndHash.salt;
user.password = saltAndHash.hash;
return cb(null, options);
}

希望这对您有帮助;)

关于javascript - Sequelize : Before Create Hook is Not Working As Intended,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46503286/

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