В качестве промежуточного программного обеспечения для аутентификации пользователей у меня есть пароль, и я также использую jwt, поэтому я хотел бы сравнить, является ли почтовый пароль тем же, что и в базе данных (сохраненный как хеш и соль).
Но это
console.log(bcrypt.compareSync(req.body.password, user.hash));
всегда верни мне ложь.
Как мне изменить его, чтобы исправить эту проблему? Есть ли способ изменить код без использования bcrypt?
Большое спасибо.
Для регистрации пользователя и входа в систему:
router.route('/register')
.get(function(req, res) {
res.render('register', { });
})
.post(function(req, res) {
Account.register(new Account({ username : req.body.username }), req.body.password, function(err, account) {
if (err) {
return res.render('register', {info: 'Sorry. That username already exists. Try again.'});
}
passport.authenticate('local')(req, res, function() {
res.redirect('/');
});
});
});
router.route('/login')
.get(function(req, res) {
res.render('login', { user: req.user });
})
.post(passport.authenticate('local'), function(req, res) {
res.redirect('/');
});
router.get('/logout', function(req, res) {
req.logout();
res.redirect('/');
});
Для аутентификации jwt у меня есть:
router.post('/authenticate', function(req, res) {
var collection = db.get('accounts');
// find the user
collection.findOne({
username: req.body.username
}, function(err, user) {
if (err) throw err;
if (!user) {
res.json({ success: false, message: 'Authentication failed. User not found.' });
} else if (user) {
// check if password matches
console.log(bcrypt.compareSync(req.body.password, user.hash));
if (!bcrypt.compareSync(req.body.password, user.hash)) {
res.json({ success: false, message: 'Authentication failed. Wrong password.' });
} else {
// if user is found and password is right
// create a token
var payload = {
admin: user.admin
}
var token = jwt.sign(payload, app.get('superSecret'), {
expiresIn: 86400 // expires in 24 hours
});
res.json({
success: true,
message: 'Enjoy your token!',
token: token
});
}
}
});
});