我目前正在尝试在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;
});
});
Run Code Online (Sandbox Code Playgroud)
能否请您解释一下哪种方式更好,为什么?谢谢!