在函数中包装 passport.authenticate 不起作用

Wrapping passport.authenticate inside a function doesn't work

我正在尝试使用 Google OAuth2Strategy 对用户进行身份验证。我有以下路线

**server.get('/user/google', passport.authenticate('google', {scope: ['openid email profile']});
server.get('/user/google/callback', authenticate.authenticateGoogleCallback);** 

这完全没问题。但是当我像我为回调所做的那样包装第一个身份验证时,它只是挂起。是错误还是我做错了什么?

这就是我正在尝试的。

**server.get('/user/google', authenticate.authenticateGoogle); // NOT WORKING
server.get('/user/google', function(req,res,next){ // NOT WORKING
     passport.authenticate('google', {scope: ['openid email profile']});
});**

试试这个,让我们知道它是否有效。 (您必须按照 link http://passportjs.org/docs 中所述在函数末尾提供 (res,req,next))

server.get('/user/google', function(req, res, next) {
  passport.authenticate('google', {
     scope: ['openid email profile']
  } ,function(err, user, info){
    res.send(user);
  })(req,res,next);
})

这就是我在我的一个项目中包装 passport.authenticate 的方式:

server.get('/user/google', (req, res, next) => {
  // making additional checks [you can skip this]
  const auth = req.header('Authorization')

  if (auth) {
    // this is what you are looking for
    passport.authenticate('jwt', { session: false })(req, res, next)
  } else {
    next()
  }
})