问题
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