我正在嘗試制作登錄功能當我創建這樣的登錄功能時
login: (req,res) => {
const body = req.body;
getUserByEmail(body.email,(err,results)=>{
if(err){
console.log(err);
}
if(!results){
return res.json({
sucess: 0,
data: "Invalid email or password 1"
});
}
console.log(body.pw);
console.log(results.pw);
const result = compareSync(body.pw,results.pw);
console.log(result)
if(result){
results.pw = undefined;
const jsontoken = sign({result: results},"1234413",{
expiresIn: "1h"
});
return res.json({
sucess: 1,
message: "login sucessfully",
token: jsontoken
});
}else{
return res.json({
sucess: 0,
data: "Invalid email or password2"
});
}
});
}
終端答案
我控制臺記錄 body.pw 和 results.pw 但似乎它給了我相同的字串但我不確定為什么即使我有相同的 body.pw 和 results.pw 字串它也會給我錯誤的結果
uj5u.com熱心網友回復:
compareSync 將密碼與哈希值進行比較。在將密碼與純文本密碼進行比較之前,您首先需要對密碼進行哈希處理。
第一次運行
bcrypt.hash(myPlaintextPassword, saltRounds).then(function(hash) {
// Store hash in your password DB.
});
然后你可以比較
bcrypt.compare(myPlaintextPassword, hash).then(function(result) {
// result == true
});
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/451102.html
標籤:javascript 节点.js 表示 验证 bcrypt
上一篇:如何防止useEffect()第一次啟動但監聽狀態變化?
下一篇:如何允許通過標頭訪問>用戶代理
