这个问题通常是由于Bcrypt比较函数的参数顺序不正确引起的。正确的格式应该是:
bcrypt.compare(plainTextPassword, hashedPassword, function(error, success) { // ... });
其中,第一个参数是明文密码,第二个参数是已哈希密码。如果两个参数反了,比较函数会始终返回false。因此,正确的参数顺序是非常重要的。
以下是一个示例,其中包括正确的参数顺序:
const bcrypt = require('bcrypt');
const plainTextPassword = 'password123'; const hashedPassword = '$2b$10$p7Q5/ljEaq8eMTQaUQDLne2CCYT0NHQokM4ZCQAjruT/AdiatbvWi';
bcrypt.compare(plainTextPassword, hashedPassword, function(error, success) { if (error) { console.log(error); } else { console.log(success); } });
在这个示例中,Bcrypt比较函数将plainTextPassword与hashedPassword进行比较,并返回true或false,具体取决于它们是否匹配。如果两个参数顺序不正确,函数将始终返回false。