gpt4 book ai didi

postgresql - bcrypt compareSync “Unhandled rejection Error: data and hash must be strings”

转载 作者:行者123 更新时间:2023-12-03 09:06:09 25 4
gpt4 key购买 nike

我正在使用knex从数据库中选择特定的用户密码。这个哈希密码在这样的对象中返回给我:

[ anonymous {
password_hash:
'$2a$10$zRSFdNJsg8S3Xv1I73gjYuRu.Mw/9Dtujh0dTqrtc9DA12vUHuqbK' } ]
当我尝试使用 bcrypt.compareSync比较输入的密码和数据库中存在的密码时,它给我一个错误,提示数据和哈希必须是字符串。我尝试使用 .toString()方法和 JSON.stringify(password_hash)。但是两者都没有帮助。我该如何工作?
这是我的代码:
knex('users').where({
email: req.body.loginEmail
}).select('password_hash')
.then(function(password_hash) {
//console.log("resp", password_hash)
console.log("JSON stringifies",JSON.stringify(password_hash))
console.log("req", req.body.loginPass)
console.log('hash', password_hash)
bcrypt.compareSync(req.body.loginPass, JSON.stringify(password_hash), function(err, doesMatch){
if (doesMatch){
console.log("Passwords Match")
req.session.email = rows[0].email;
var templateVars = {
emale: req.session.email
}
res.render('/dashboard', templateVars)
} else {
console.log("THIS IS THE ERROR", err);
res.send("I DONT KNOW YOU. SIGN UP FIRSt.")
}
})
})
我的整个错误:
Unhandled rejection Error: data and hash must be strings
at Object.compareSync (/vagrant/CaloBoxInc/node_modules/bcrypt/bcrypt.js:144:15)
at /vagrant/CaloBoxInc/server.js:159:14
at tryCatcher (/vagrant/CaloBoxInc/node_modules/bluebird/js/release/util.js:16:23)
at Promise._settlePromiseFromHandler (/vagrant/CaloBoxInc/node_modules/bluebird/js/release/promise.js:512:31)
at Promise._settlePromise (/vagrant/CaloBoxInc/node_modules/bluebird/js/release/promise.js:569:18)
at Promise._settlePromise0 (/vagrant/CaloBoxInc/node_modules/bluebird/js/release/promise.js:614:10)
at Promise._settlePromises (/vagrant/CaloBoxInc/node_modules/bluebird/js/release/promise.js:693:18)
at Async._drainQueue (/vagrant/CaloBoxInc/node_modules/bluebird/js/release/async.js:133:16)
at Async._drainQueues (/vagrant/CaloBoxInc/node_modules/bluebird/js/release/async.js:143:10)
at Immediate.Async.drainQueues (/vagrant/CaloBoxInc/node_modules/bluebird/js/release/async.js:17:14)
at runCallback (timers.js:570:20)
at tryOnImmediate (timers.js:550:5)
at processImmediate [as _immediateCallback] (timers.js:529:5)

最佳答案

可能是您的req.body.loginPass不是字符串吗? JSON.stringify始终返回undefined/string,如果参数未定义,则bcrypt返回不同的错误,因此参数不会成为问题。

尝试强制将req.body.loginPass强制转换为这样的字符串:

bcrypt.compareSync(´´+ req.body.loginPass, ...

(仅用于调试目的...尽管您应该已经从早期的控制台日志中看到它不是字符串)。

关于postgresql - bcrypt compareSync “Unhandled rejection Error: data and hash must be strings”,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44063440/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com