NodeJs passport 本地策略认证

NodeJs passport local strategy authentication

我想在我的 NodeJs Web 应用程序中使用 passport localStrategy 创建非常简单的身份验证。

app.post('/login', function(req, res) {
  console.log('before auth');
  passport.authenticate('local'),
    function(req, res) {
      // If this function gets called, authentication was successful.
      // `req.user` contains the authenticated user.
      // res.redirect('/users/' + req.user.username);
      console.log('auth is ok');
    }
});

我做了什么:

  1. 我有一个包含登录名和密码字段以及操作 = "/login"

  2. 的 Web 表单
  3. 在我应用程序的路由器中,我有这样的登录路径

提交表单后,我可以在我的控制台中看到 "before auth",这意味着路由器正在工作。但是我看不到"auth is ok"这意味着认证没有成功

如何在我的应用程序中实现 passport.authenticate 功能?

var passport = require('passport');
var LocalStrategy = require('passport-local').Strategy;

  passport.use(new LocalStrategy({
    usernameField: 'email',
    passwordField: 'password' 
  }, function(email, password, next) {
    //do what you want here
    //call next(pass parameter here (i.e error, object)
  }));


app.post('/login', function(req, res) {
      console.log('before auth');
      passport.authenticate('local', function(err, anotherthing){
      //err and anotherThing are the parameters send by strategy (next function). 

       });
    });

也看看here。有关完成此操作的更多详细信息。亲切的问候