データベースから特定のユーザーのパスワードを選択するのにknexを使用しています。このハッシュ化されたパスワードは、次のようにオブジェクトに私に返されます。bcrypt compareSync "未処理の拒否エラー:データとハッシュは文字列でなければなりません"
[匿名{ 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)
問題を再現するrunkitなどの完全なテストケースを追加できますか? –
pls、あなたの質問をフルコードで更新してください。 –