【问题标题】:How to process passport authenticate function in as request handler function如何在请求处理函数中处理护照身份验证功能
【发布时间】:2016-09-18 11:31:27
【问题描述】:

我正在创建一个基于 Node.js 和 Express 4 的 Web 应用程序。我还使用 Passportjs 和 Google OAuth 2 Startegy 进行身份验证。

我正在尝试配置我的路由以处理请求。 我了解到这条线路运行良好:

router.get('/signin/google/callback', passport.authenticate('google', {failureRedirect: '/signin'}));

但是当我决定在函数中处理路由时,应用程序停止响应:

router.get('/signin/google/callback', function (req, res) {
    passport.authenticate('google', {failureRedirect: '/signin'});
});

我错过了什么吗?提前致谢

【问题讨论】:

    标签: node.js express passport.js middleware passport-google-oauth2


    【解决方案1】:

    Google OAuth 函数的回调应该是这样的:

    app.get('/auth/google/callback', 
      passport.authenticate('google', { failureRedirect: '/login' }),
      function(req, res) {
        res.redirect('/');
      });
    

    passport.authenticate() 是带有参数request,response, next 的中间件。您还可以定义自己的中间件或最后一个请求处理程序。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-10-01
      • 2018-03-27
      • 1970-01-01
      • 2015-06-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多