NodeJs护照本地策略认证

Alex Zhulin

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

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. 我有一个包含登录名和密码以及action =“ / login”的Web表单

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

提交表单后,我可以在控制台中看到“ auth之前”,这表示路由器正在工作。但是我看不到“身份验证正常”,这表示身份验证未成功。

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

埃尔默·丹塔斯(Elmer Dantas)
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). 

       });
    });

也可以在这里看看有关更多详细信息,请完成此操作。亲切的问候

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章