【问题标题】:Google auth with passport callback not being reached未达到护照回调的谷歌身份验证
【发布时间】:2018-02-13 06:06:16
【问题描述】:

基于https://github.com/passport/express-4.x-facebook-example/blob/master/server.js,我正在尝试使用 Passport 和 node.js 实现基本的 Google 身份验证。

我的 Passport 配置如下所示:

passport.use(new GoogleStrategy({
    clientID: GOOGLE_CLIENT_ID,
    clientSecret: GOOGLE_CLIENT_SECRET,
    callbackURL: "https://localhost:3000/auth/callback" // not sure if correct.
  },
  function(accessToken, refreshToken, profile, cb) {
    console.log("accessToken: " + accessToken);
    console.log("refreshToken: " + refreshToken);
    console.log("profile: " + profile);
    console.log("cb: " + cb);
    cb(accessToken, profile);
  }
));

我已经设置了以下 GET 路由:

app.get('/auth/',
  passport.authenticate('google', { scope: ['profile'] }));

app.get('/auth/callback', 
  passport.authenticate('google', { failureRedirect: '/login' }),
  function(req, res) {
    console.log("success");
    // Successful authentication, redirect home.
    res.redirect('/');
  });

当我尝试访问身份验证页面时,我得到一个正常的谷歌登录屏幕,但在我登录后,我得到一个net::ERR_CONNECTION_CLOSED 消息。该 URL 看起来像 https://localhost:3000/auth/callback?code=ACCESS_CODE,但不知何故不会导致重定向到 /login(如果失败)或重定向到 /(如果成功)。

感谢任何帮助!我对 Passport 和一般的 oauth 都很陌生。

【问题讨论】:

    标签: node.js oauth-2.0 google-authentication passport.js


    【解决方案1】:

    根据passport oauth2 library documentation,passport中间件回调的第一个参数是错误的。所以,你的代码:

    cb(accessToken, profile)
    

    被视为错误并带有 accessToken 'error'。这大概是个问题。您需要将其更改为:

    cb(null, profile)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-11-06
      • 2020-10-14
      • 1970-01-01
      • 2018-12-13
      • 2017-04-24
      • 2014-08-18
      相关资源
      最近更新 更多