问题描述
我正在尝试构建一个待办事项应用程序,在该应用程序中有一个注册和登录页面,我试图对其进行哈希处理并在用户尝试登录时对其进行比较。最初使用bcrypt sync方法我可以对密码进行哈希处理,但是我的Scompare函数始终返回false。然后我将其更改为async方法,发现它没有被哈希处理,甚至compare函数也无法正常工作。有人可以帮忙吗?
databases / sqlite.js:
const Sequelize = require('sequelize');
const bcrypt = require('bcrypt');
const saltRounds = 10;
const sequelize = new Sequelize({
dialect: "sqlite", storage: path.join(__dirname,"database.sqlite")
});
const User= sequelize.define("users",{
name: {
type: Sequelize.STRING, }, email: {
type: Sequelize.STRING, unique: true, password: {
type: Sequelize.STRING, }},{
hooks: {
beforeCreate: (user) => {
/*const salt = bcrypt.genSaltSync();
user.password = bcrypt.hashSync(user.password,salt);*/
bcrypt.hash(user.password,saltRounds,function(err,hash) {
// Store hash in your password DB.
if(err) console.log("something wrong with hashing",err)
user.password=hash;
});
}
},instanceMethods: {
validPassword: function(password) {
bcrypt.compare(password,user.password,result) {
if (err) { throw (err); }
console.log(result);
return result;
});
}
//return bcrypt.compareSync(password,this.password);
}
});
sequelize
.sync()
.then(() =>
console.log(
"users table has been successfully created,if one doesn't exist"
)
)
.catch(error => console.log("This error occurred"));
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)