当 Google 在 passportjs 上进行身份验证时,自定义回调从未调用过

Custom Callback never called when Google Auth on passportjs

我尝试使用 PassportJS 以 Google 登录。但是当我使用自定义回调时,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 日志。

提前致谢。

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

passport.authenticate方法,returns函数。

如果你这样使用,你必须自己调用它。

看:

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是一个中间件。

希望能有所帮助。