什么是“完成” Passport策略中的回调函数配置“使用”功能 [英] What is "done" callback function in Passport Strategy Configure "use" function

查看:81
本文介绍了什么是“完成” Passport策略中的回调函数配置“使用”功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是node.js和express.js noob。这个问题看似愚蠢但我真的很困惑。

I'm an node.js and express.js noob. This question may seems silly but I'm really in confusion.

我正在尝试配置护照进行本地>本地策略认证。如官方文档所示,我们可以通过以下代码来计算本地策略,

I'm trying to configure Local Strategry authentication by using passport. As shown in the official documentation, we can figure this Local Strategy by the following code,

passport.use(new LocalStrategy(
  function(username, password, done) {
    User.findOne({ username: username }, function (err, user) {
      if (err) { return done(err); }
      if (!user) { return done(null, false); }
      if (!user.verifyPassword(password)) { return done(null, false); }
      return done(null, user);
    });
  }
));

我的困惑是关于已完成回调函数。当官方文档显示此局部策略在路由处理程序中用作中间件时,不需要为此 done 回调传递函数参数。

My confusion is about the done callback function. When the official docs show this local strategy using as a middleware in the route handler, there is no need to pass the function parameter for this done callback.

app.post('/login', 
  passport.authenticate('local'),
  function(req, res) {
    res.redirect('/');
  });

所以,这不是已完成回调如果我们不提供函数参数,函数将为null?如果没有,那个完成回调函数是什么以及这个完成回调函数会发生什么过程?

So, isn't this done callback function will be null if we don't provide the function parameter? If not, what is that done callback function and what processes will be happening in this done callback function?

推荐答案

已完成是一种方法由策略实施内部调用

然后导航正如您所见,您可以看到其中一个成功 / 错误 / 失败方法(再次,通过实现。还有更多选项)。
这些选项中的每一个都可以来电 next ,您的代码段中的内容如下:

Then it navigates you, as you can see, to one of the success / error / fail methods (again, by the implementation. there are more options). Each of these options may calls to the next, where in your snippet code is the following:

function(req, res) {
  res.redirect('/');
});

成功被调用时,它可以将用户附加到请求或做其他事情,具体取决于您的需求(它查找传递给 passport.authenticate 选项)。如果您想确定何时调用 next ,您应该使用 自定义回调 ,为您提供更大的灵活性。

When success is called, it can attach the user to the request or do other things, depending on your needs (it looks for the options you pass to passport.authenticate). If you want to determine when next will be called, you should use custom callback which gives you more flexibility.

我强烈建议您阅读来源。

I strongly recommend that you read the source.

这篇关于什么是“完成” Passport策略中的回调函数配置“使用”功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆