我目前正在尝试在 node.js 中为我的项目制作身份验证模块?
我已经看到了一些使用 bcrypt 生成哈希的例子,即
https://github.com/bnoguchi/mongoose-auth/blob/master/lib/modules/password/plugin.js https://github.com/Turbo87/locomotive-passport-boilerplate/blob/master/app/models /account.js
但是,出于某种原因,他们正在使用 bcrypt.hashSync() 函数。由于 bcrypt 很好,因为它很耗时,所以为了不阻塞代码,使用异步函数不是更明智,即:
User.virtual('password')
.get( function () {
return this.hash;
})
.set( function (password) {
bcrypt.hash('password', 10, function(err, hash) {
this.hash = hash;
});
});
你能解释一下哪种方式更好,为什么?谢谢!