未调用Google策略匿名功能

时间:2018-07-24 22:26:43

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

我正在尝试使用护照,以便用户可以使用其Google帐户登录到我的网站。我在以下相关程序包中使用yarn: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错误。此后,将请求GET请求pug文件中引用的所有根页面文件,只是将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)

这是根目录的快速文件中的相关代码:

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');均未执行。单击退出锚点并执行名为afterwords的重定向功能时,将执行console.log('logged out');

由于可能的原因,该策略未运行匿名回调函数,这是我一直在查看的大多数资源:

1 个答案:

答案 0 :(得分:0)

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

  

登录按钮和callbackURL都必须共享相同的URL,否则将创建两个cookie,这将导致会话丢失

这暗示Google oauth 2.0需要会话。