【发布时间】:2021-06-04 17:24:19
【问题描述】:
伙计们,我正在尝试使用 BCRYPT 制作密码哈希,但用户真实密码已发送到 PostgresSQL 我能做些什么来解决它?我写错了吗?
import { Model, Sequelize } from 'sequelize';
const bcrypt = require('bcrypt');
class User extends Model {
static init(sequelize) {
super.init(
{
name: Sequelize.STRING,
email: Sequelize.STRING,
password: Sequelize.VIRTUAL,
password_hash: Sequelize.STRING,
provider: Sequelize.BOOLEAN,
},
{
sequelize,
}
);
this.addHook('beforeSave', async (user) => {
if (user.password) {
user.password_hash = await bcrypt.hash(user.password, 8);
}
return this;
});
}
}
export default User;
【问题讨论】:
-
为什么您的用户表中有 2 个密码字段?删除“password”然后你将只有“password_hash”。
-
“密码”我将由用户创建,“password_hash”我将基于用户“密码”创建,这就是我使用bcrypt的原因。
标签: javascript node.js postgresql hash sequelize.js