我正在关注nodejs和expressjs与项目的教程,该项目是用户登录/注册/注销系统。我坚持使用passportjs登录,即使一切都正确,也没有调用LocalStartegy对象,因为我删除了它并记录了一条消息;我知道它没有被调用,因为它没有记录任何东西
这是代码
passport.serializeUser(function(user, done) {
done(null, user.id);
});
passport.deserializeUser(function(id, done) {
User.getUserById(id, function(err, user) {
done(err, user);
});
});
//local startegy
passport.use(new LocalStrategy({
usernameField: 'login',
passwordField: 'password'
},
function(username, password,done){
console.log('local startegy called!');
User.getUserByUsername(username, function(err, user){
if (err) { return done(err); }
if(!user){
console.log('Unknown User');
return done(null, false, { message: 'Incorrect username.' });
}
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'});
}
});
});
}
));
router.post('/login', passport.authenticate('local', {failureRedirect:
'/users/login', failureFlash: 'Invalid username or password'}), function(req, res){
//runs if the local startegy comes back true
console.log('Authentication successfull...');
req.flash('success', 'You are logged in');
res.redirect('/');
});
这是我的表格
extends layout
block content
h1 Login
p Login if you are already a member
form(method='post', action='/users/login', multipart='urlencoded')
.form-group
label Username
input.form-control(name='username', type='text',
placeholder='username')
.form-group
label Password
input.form-control(name='password', type='password',
placeholder='password')
input.btn.btn-default(name='submit', type='submit', value='login')