【问题标题】:Google Strategy Anonymous Function Not Being Called谷歌策略匿名函数未被调用
【发布时间】:2019-01-01 15:40:02
【问题描述】:

我正在尝试使用护照,以便用户可以使用他们的谷歌帐户登录到我的网站。我正在使用带有这些相关包的纱线:passport@0.4.0 和 passport-google-oauth20@^1.0.0。一般问题似乎是GoogleStrategy 中指定的回调函数没有运行,因此服务器正在尝试加载一个不存在的页面,从而导致几个错误。

当前发生的情况是我使用 nodemon 启动服务器,将 Google 客户端 ID 和机密传递给进程变量,这些变量的值被 GoogleStrategy 使用(我已在控制台记录它们以确保它们被正确传递)。然后我在浏览器中访问根页面并单击登录锚点,它会得到/auth/google。将出现显示可能用于登录的帐户的 google 登录屏幕。我选择了我的帐户,然后服务器收到一个到 /auth/google/callback?code=4/(a long string of characters) 的 GET 请求,其中有一个 500 错误与之相关联。此后,pug 文件中引用的所有根页面文件都被 GET 请求,除了样式表之外的每个请求的前面都添加了auth/google

示例输出:

GET /auth/google 302 1.893 ms - 0
GET /auth/google/callback?code=4/(long string of text) 500 178.246 ms - 3087
GET /auth/google/node_modules/jquery/dist/jquery.js 404 38.048 ms - 3087
GET /auth/google/node_modules/bootstrap/dist/css/bootstrap.css 404 65.666 ms - 3087
GET /auth/google/node_modules/angular-material/angular-material.css 404 89.565 ms - 3087
GET /auth/google/node_modules/angular/angular.js 404 115.541 ms - 3087
GET /auth/google/node_modules/angular-animate/angular-animate.js 404 141.761 ms - 3087
GET /auth/google/node_modules/angular-messages/angular-messages.js 404 161.489 ms - 3087
GET /auth/google/node_modules/angular-material/angular-material.js 404 23.809 ms - 3087
GET /auth/google/node_modules/angular-aria/angular-aria.js 404 43.268 ms - 3087
GET /auth/google/apps/angular_app.js 404 62.520 ms - 3087
GET /stylesheets/style.css 304 86.279 ms - -

此时的页面似乎只有其主 pug 文件和 css 文件中的内容;例如,没有一个 ng-repeats 或 ng-includes 起作用。此时浏览器中的URL为http://(my domain)/auth/google/callback?code=4/(a long string of characters)

下面是根页面 express 文件中的相关代码:

var passport = require('passport');
var GoogleStrategy = require('passport-google-oauth20').Strategy;

console.log('google client id: ' + process.env.GOOGLE_CLIENT_ID);
console.log('google client secret: ' + process.env.GOOGLE_CLIENT_SECRET);
router.use(passport.initialize());

passport.use(new GoogleStrategy({
  clientID: process.env.GOOGLE_CLIENT_ID,
  clientSecret: process.env.GOOGLE_CLIENT_SECRET,
  callbackURL: 'http://(my domain)/auth/google/callback'
},
function(accessToken, refreshToken, profile, done) {
  console.log('start of callback');
  return done(null, profile);
}));

app.use(passport.initialize());

router.get('/auth/google', passport.authenticate('google', {scope: ['profile']} ));
router.get('/auth/google/callback', passport.authenticate('google', {
  failureRedirect: '/',
  function(req, res, next) {
    res.redirect('/');
  }
}));

router.get('/logout', function(req, res) {
  console.log('logged out');
  req.logout();
  res.redirect('/');
});

module.exports = router;

console.log('start of callback'); 在第二段序列的任何点都没有被执行。当点击注销锚点时,console.log('logged out'); 正在被执行,并且名为 afterwords 的重定向函数也被执行。

以下是我一直在寻找的大多数来源,可能是因为该策略没有运行匿名回调函数:

【问题讨论】:

    标签: javascript express authentication passport.js google-authentication


    【解决方案1】:

    我最终尝试添加会话功能,以查看 Passport 和 Google oauth 2.0 的组合是否需要它。就是这样,添加基本会话功能后,回调函数被调用,流程完成。我没有找到说明需要会话的文档,但是在重新阅读了 GitHub 存储库中的“自述”页面后,该页面链接在我上面的问题中,

    另外,登录按钮+callbackURL 必须共享同一个url,否则会创建两个cookie,导致会话丢失

    暗示 Google oauth 2.0 需要会话。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-01-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-09-11
      • 1970-01-01
      • 2013-02-23
      相关资源
      最近更新 更多