我的项目有问题。我正在使用Postgres数据库(9.6)。在我的项目中,我正在创建应用程序用户对象,然后使用BCrypt哈希其密码。之后,将其插入数据库。但是,当我尝试登录时,数据库的哈希值和通过凭据创建的哈希值不匹配,因此无法登录。当我尝试连接到另一种数据库类型(MariaDB)时,它可以正常工作。我不知道我在做什么错,但是必须在数据库连接中配置一些错误。
//this is my connection configuration
db: {
hostname: "localhost",
dialect: "postgres",
username: "user_turnaj",
password: "heslo",
database: "turnajovka",
logging: false
}
这是我的User对象的外观
const bcrypt = require("bcrypt");
const hashPassword = password => {
return bcrypt.hashSync(password, bcrypt.genSaltSync());
};
module.exports = (sequelize, DataTypes) => {
const model = sequelize.define("User", {
username: {
type: DataTypes.STRING,
unique: true,
allowNull: false
},
password: {
type: DataTypes.CHAR,
length: 60,
allowNull: false
}
}, {
hooks: {
beforeCreate: user => {
const hashed = hashPassword(user.password);
console.log(hashed);
user.password = hashed;
}
}
});
model.prototype.hashPassword = function (password) {
return hashPassword(password);
};
model.prototype.validPassword = function (password) { //this is where the hashes are not the same
return bcrypt.compareSync(password, this.password);
};
return model;
};
这是带有用户的表格的外观,它是由我的模型用户自动生成的。
-- auto-generated definition
create table users
(
id serial not null
constraint users_pkey
primary key,
username varchar(255) not null
constraint users_username_key
unique,
name varchar(255) not null,
password char(255) not null,
email varchar(255) not null
constraint users_email_key
unique,
gender enum_users_gender,
role enum_users_role default 'USER'::enum_users_role,
created_at timestamp with time zone not null,
updated_at timestamp with time zone not null
);
alter table users
owner to user_turnaj;