【问题标题】:Passport authenticate, need basic explanation护照认证,需要基本解释
【发布时间】:2019-03-28 13:18:03
【问题描述】:

谁能解释一下我们在passport.authenticate的末尾包含什么以及为什么要包含(req,res,next),到目前为止我在教程中看到的只是我们需要它,因为我们希望它触发立即关闭,但我真的不明白。

代码如下:

router.post('/login', (req, res, next) => { <br>
    passport.authenticate('local', { <br>
        successRedirect: '/songs/list', <br>
        failureRedirect: '/users/login', <br>
        failureFlash: true <br> 
    })(req, res, next);                  <---- *This line*

【问题讨论】:

    标签: node.js authentication passport.js passport-local


    【解决方案1】:

    您正在声明一个函数并立即调用它。您这样做是为了访问passport.authenticate 中的req 对象。

    所以如果你需要访问passport里面的请求对象你需要一个自定义回调。你的代码好像没有使用req对象,所以你简单使用

    app.post("/protected",passport.authenticate("local",{
            successRedirect:"/user",
            failureRedirect:"/login"
        }),function(req,res){
    });
    

    如果您的应用程序需要访问 req 对象,那么:

    app.get('/protected', function(req, res, next) {
      passport.authenticate('local', function(err, user, profile) {
        if (err) { return next(err) }
        if (!user) { return res.redirect('/signin') }
        res.redirect('/account');
      })(req, res, next);
    });
    

    更多信息@https://github.com/jaredhanson/passport/issues/1

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-03-28
      • 2013-06-09
      • 1970-01-01
      • 1970-01-01
      • 2022-08-05
      • 2016-10-12
      • 2015-09-23
      • 2019-03-01
      相关资源
      最近更新 更多