2017-06-05 25 views
0

我尝试使用PassportJS与Google登录。但是当我使用自定义回调时,Google策略从不称呼回调。我究竟做错了什么?我的代码如下。当Google Passh on passportjs时,自定义回拨从未呼叫

端点:

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回调日志打印,但打印从未资料登入。

在此先感谢。

回答

1

这是一招的情况...

passport.authenticate方法,返回的功能。

如果你以这种方式使用,你必须自己调用它。

看:

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

希望是帮助。

+0

非常感谢。你救了我的命。它正在工作。 – endrcn