And*_*kiy 8 asynchronous bcrypt node.js
我目前正在尝试在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)
能否请您解释一下哪种方式更好,为什么?谢谢!
是的,如果可能的话,您希望使用异步版本,这样您就不会在密码哈希期间占用节点处理.在您引用的两个源代码中,代码都使用同步版本,因为它在其中使用的方法是同步的,因此作者别无选择,只能使用同步版本.
| 归档时间: |
|
| 查看次数: |
5958 次 |
| 最近记录: |