Bcrypt compare returns false randomly for correct passwords
Bcrypt compare returns false randomly for correct passwords
我在 MERN 项目 (Node v8.10.0) 中使用 bcrypt (v2.0.1) 使用 Mongoose 在 MongoDB 中存储用户密码。哈希函数:
SellerSchema.pre('save', function (next) {
var user = this;
bcrypt.hash(user.password, 10, function (err, hash) {
if (err) {
return next(err);
}
user.password = hash;
next();
});
});
认证部分:
SellerSchema.statics.authenticate = function (email, password, callback) {
Seller.findOne({ email: email })
.exec(function (error, user) {
if (error) return callback(error);
else if (!user) {
var err = new Error('User not found.');
err.status = 401;
return callback(err);
}
bcrypt.compare(password, user.password).then(function (result){
if (result == true) {
return callback(null, user);
} else {
return callback();
}
});
});
};
登录路径:
router.post('/login', function(req, res, next) {
if (req.body.email && req.body.password){
Seller.authenticate(req.body.email,req.body.password,function(error,user) {
if (error || !user) {
console.log("this "+error);
var err = new Error('Wrong email or password.'); // logged everytime the compare result was false
err.status = 401;
return next(err);
} else {
res.json(user);
}
});
} else {
var err = new Error('Email and password are required.');
err.status = 401;
return next(err);
}
});
注册新用户时,哈希存储良好并使用纯文本登录 确实 几次通过了 compare(),但是 returns之后是假的。
如果我注册一个新用户并登录,它会再次运行一段时间然后开始返回 false。
例如:我仍然可以登录几小时前注册的用户(比较 10-15 次),但无法登录几分钟前创建的用户(比较 1-2 次后)
使用节点:8.10.0,OS:Ubuntu18.04
你的 pre('save')
中间件在你每次保存时更新密码 object.To 停止使用 isModified
猫鼬的功能,如下所示:
SellerSchema.pre('save', function(next) {
if (this.isModified('password')) { // check if password is modified then has it
var user = this;
bcrypt.hash(user.password, 10, function(err, hash) {
if (err) {
return next(err);
}
user.password = hash;
next();
});
} else {
next();
}
});
我在 MERN 项目 (Node v8.10.0) 中使用 bcrypt (v2.0.1) 使用 Mongoose 在 MongoDB 中存储用户密码。哈希函数:
SellerSchema.pre('save', function (next) {
var user = this;
bcrypt.hash(user.password, 10, function (err, hash) {
if (err) {
return next(err);
}
user.password = hash;
next();
});
});
认证部分:
SellerSchema.statics.authenticate = function (email, password, callback) {
Seller.findOne({ email: email })
.exec(function (error, user) {
if (error) return callback(error);
else if (!user) {
var err = new Error('User not found.');
err.status = 401;
return callback(err);
}
bcrypt.compare(password, user.password).then(function (result){
if (result == true) {
return callback(null, user);
} else {
return callback();
}
});
});
};
登录路径:
router.post('/login', function(req, res, next) {
if (req.body.email && req.body.password){
Seller.authenticate(req.body.email,req.body.password,function(error,user) {
if (error || !user) {
console.log("this "+error);
var err = new Error('Wrong email or password.'); // logged everytime the compare result was false
err.status = 401;
return next(err);
} else {
res.json(user);
}
});
} else {
var err = new Error('Email and password are required.');
err.status = 401;
return next(err);
}
});
注册新用户时,哈希存储良好并使用纯文本登录 确实 几次通过了 compare(),但是 returns之后是假的。
如果我注册一个新用户并登录,它会再次运行一段时间然后开始返回 false。 例如:我仍然可以登录几小时前注册的用户(比较 10-15 次),但无法登录几分钟前创建的用户(比较 1-2 次后)
使用节点:8.10.0,OS:Ubuntu18.04
你的 pre('save')
中间件在你每次保存时更新密码 object.To 停止使用 isModified
猫鼬的功能,如下所示:
SellerSchema.pre('save', function(next) {
if (this.isModified('password')) { // check if password is modified then has it
var user = this;
bcrypt.hash(user.password, 10, function(err, hash) {
if (err) {
return next(err);
}
user.password = hash;
next();
});
} else {
next();
}
});