本文介绍了将passport.authenticate 包装在函数中不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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?

这就是我正在尝试的.

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

推荐答案

试试这个,让我们知道它是否有效.(您必须在功能结束时提供 (res,req,next),如本链接中所述http://passportjs.org/文档)

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

这篇关于将passport.authenticate 包装在函数中不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-19 05:05