【问题标题】:Custom Callback never called when Google Auth on passportjs当 Google Auth on passportjs 从未调用自定义回调
【发布时间】:2017-11-06 02:34:04
【问题描述】:

我尝试使用 PassportJS 登录 Google。但是当我使用自定义回调时,谷歌策略从未调用回调。我究竟做错了什么?我的代码如下。

端点:

var router = express.Router();
router.get('/',
  passport.authenticate('google', { scope: [
    'https://www.googleapis.com/auth/plus.login',
    'https://www.googleapis.com/auth/plus.profiles.read',
    'https://www.googleapis.com/auth/userinfo.email'
  ] }
));

router.get('/callback', function (req, res) {
  console.log("GOOGLE CALLBACK");
  passport.authenticate('google', function (err, profile, info) {
    console.log("PROFILE: ", profile);
  });
});

module.exports = router;

护照:

passport.use(new GoogleStrategy({
          clientID: config.GOOGLE.CLIENT_ID,
          clientSecret: config.GOOGLE.CLIENT_SECRET,
          callbackURL: config.redirectURL+"/auth/google/callback",
          passReqToCallback: true
          },
          function(request, accessToken, refreshToken, profile, done) {
            process.nextTick(function () {
              return done(null, profile);
            });
          }
        ));

GOOGLE CALLBACK 日志已打印,但 PROFILE 日志从未打印。

提前致谢。

【问题讨论】:

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


    【解决方案1】:

    这是一个诡计的情况......

    passport.authenticate 方法,返回一个函数。

    如果你以这种方式使用,你必须自己调用它。

    看:

    router.get('/callback', function (req, res) {
      console.log("GOOGLE CALLBACK");
      passport.authenticate('google', function (err, profile, info) {
        console.log("PROFILE: ", profile);
      })(req, res); // you to call the function retuned by passport.authenticate, with is a midleware.
    });
    

    或者,您可以这样做:

    router.get('/callback', passport.authenticate('google', function (err, profile, info) {
        console.log("PROFILE: ", profile);
      }));
    

    passport.authenticate 是一个中间件。

    希望有帮助。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-12-10
      • 2014-06-24
      • 1970-01-01
      • 2023-03-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-09-30
      相关资源
      最近更新 更多