0
我正在使用passport.js爲我的應用程序的我的node.js後端驗證用戶身份。下面的代碼總是執行failureRedirect,我無法找到它的原因。沒有錯誤消息。Passport身份驗證始終執行failureRedirect
router.post('/login', passport.authenticate('local', {
failureRedirect: '/users/login',
failureFlash: 'Invalid email or password'
}), function(req, res) {
console.log('Authentication Successful');
req.flash('success', 'You are logged in ');
res.redirect('/');
});
我從Passport網站複製此代碼,這也太不工作:
router.post('/login', passport.authenticate('local', { successRedirect: '/',
failureRedirect: '/users/login' }));
下面的代碼甚至沒有開始:
passport.use(new localStrategy({
email: 'email',
password: 'password'
}, function(email, password, done) {
User.getUserByEmail(email, function(err, user) {
if (err) throw err;
if (!user) {
console.log('Unknown User');
return done(null, false, {
message: 'Unknown User'
});
}
User.comparePassword(password, user.password, function(err, isMatch) {
if (err) throw err;
if (isMatch) {
return done(null, user);
} else {
console.log('Invalid Password');
return done(null, false, {
message: 'Invalid Password'
});
}
});
});
}));
相關代碼的休息:
passport.serializeUser(function(user, done) {
done(null, user.id);
});
passport.deserializeUser(function(id, done) {
User.getUserById(id, function(err, user) {
done(err, user);
});
});
module.exports.getUserByEmail = function(email, callback){
var query = {email: email};
User.findOne(query, function(err, user) {
callback(err, user);
});
}
module.exports.getUserById = function(id, callback){
User.findById(id, function(err, user) {
callback(err, user);
});
}
module.exports.comparePassword = function(userPassword, hash, callback){
console.log("pwd: " + userPassword + " hash: " + hash);
bcrypt.compare(userPassword, hash, function(err, isMatch) {
if(err) return callback(err);
callback(null, isMatch);
});
}
不,這是行不通的...錯誤沒有改變PS。在你的回答中有額外的';' – suku
我的不好,我編輯答案 –
這樣一個愚蠢的事情要做 – suku