2016-11-04 76 views
-1

我正在使用yortus'async/await node.js库来设置基于Passport的登录/注册系统,并使用密码散列使用bcrypt-nodejs设置bcrypt以使用Passport和异步等待Node.js库

而系统的其余部分是安装并正常工作,我遇到了麻烦bcrypt-nodejs与yortus' async/await很好地工作,因为bcrypt哈希函数签名需要两个回调和目前还不清楚如何与async/await使用它。 ..

hash(data, salt, progress, cb) 
    data - [REQUIRED] - the data to be encrypted. 
    salt - [REQUIRED] - the salt to be used to hash the password. 
    progress - a callback to be called during the hash calculation to signify progress 
    callback - [REQUIRED] - a callback to be fired once the data has been encrypted. 
     error - First parameter to the callback detailing any errors. 
     result - Second parameter to the callback providing the encrypted form. 

async/await电话...

let generateHash = async((password) => { 
    let salt = await(bcrypt.genSaltAsync(10)) // this works 
    let result = await(bcrypt.hash(password, salt, null) 
    return result // returns NULL 
}) 

输出:

Unhandled rejection No callback function was given. 

数据库条目(MongoDB的),当我尝试使用该系统注册:

{ 
    "_id" : ObjectId("581bf7031386f167a09851b9"), 
    "username" : "vjk2005", 
    "password" : "" 
} 

用户名经历,但密码是NULL。我尝试了一些排列组合和没有成功,任何帮助表示赞赏。

回答

1

我建议仅在节点7上使用babel关键字或async/await关键字或--harmony-async-await标志。将使代码更清洁,并且我认为很少有人使用该库。他们接受了这个想法,并将其作为语言的一部分。

即使使用该库,它也需要promisified函数,而不是使用回调函数。您可以使用pify模块或查找promisified bcrypt。始终在npmjs.com或npms.io上搜索。搜索类似'bcrypt诺言'。

import {hash} from 'bcrypt-as-promised'; 

const generateHash = async password => await hash(password, 10); 

generateHash('abc1234').then(console.log).catch(console.error); 

或者因为你可以自动生成一样,您可以将其简化为盐:我实际使用蓝鸟到promisify的bcrypt LIB

import {hash} from 'bcrypt-as-promised'; 

hash('abc123',10).then(console.log).catch(console.error); 
+0

。我会保留和谐和巴贝尔作为我的最后手段。 – vjk2005