Wrapping passport.authenticate inside a function doesn't work

老子叫甜甜 提交于 2019-12-08 07:09:10

问题


I am trying to authenticate user using Google OAuth2Strategy. I have below routes

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

and this works completely fine. but when I wrap the first authenticate like how i have done for callback, it just hangs. It it a bug or i am doing something wrong?

This is what i am trying.

**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']});
});**

回答1:


Try this and let us know if its work. (you have to provide (res,req,next) in end of function as described in this link http://passportjs.org/docs)

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);
})



回答2:


This is how I wrapped the passport.authenticate in one of my project:

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()
  }
})


来源:https://stackoverflow.com/questions/36327284/wrapping-passport-authenticate-inside-a-function-doesnt-work

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!