【问题标题】:Passport-Google-OAuth Callback Not workingPassport-Google-OAuth 回调不起作用
【发布时间】:2014-11-30 21:12:05
【问题描述】:

我有以下使用 passport-google-oauth 的节点代码...

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

app.get('/auth/google/callback', function(req,res) {
    console.log("callback");
    passport.authenticate('google', {
                successRedirect : '/signin',
                failureRedirect : '/signin'
    });
});

还有……

passport.serializeUser(function(user, done) {
    console.log("ser");
    done(null, user.id);
});

passport.deserializeUser(function(id, done) {
    console.log("des");
    User.findById(id, function(err, user) {
        done(err, user);
    });
});

passport.use(new GoogleStrategy({

    clientID        : 'id',
    clientSecret    : 'key',
    callbackURL     : 'http://host/auth/google/callback',
},
function(token, rtoken, profile, done) {
   console.log("proc");
   console.log(profile);
   done(null, profile);
}));

问题是,回调被调用,但没有其他反应。处理功能永远不会命中。回调最终超时。有什么想法我哪里出错了吗?

【问题讨论】:

  • console.log("callback"); 执行了吗?
  • 另外你的 /signin 路由是什么样的?回调也应该是获取或发布。我认为这通常是一个帖子。
  • 也许您的问题出在 GoogleStrategy。试试这个: var GoogleStrategy = require('passport-google-oauth').OAuth2Strategy;

标签: javascript node.js passport.js


【解决方案1】:

您没有在第二条路由“/auth/google/callback”中使用“passport.authenticate('google')”中间件。

你的第二条路线应该是这样的:

app.get( '/auth/google/callback', 
    	passport.authenticate( 'google', { 
    		successRedirect: '/',
    		failureRedirect: '/login'
}));

【讨论】:

    【解决方案2】:

    我刚刚发现 passport-google-oauth 包导出以下内容:

    exports.Strategy =
    exports.OAuthStrategy = OAuthStrategy;
    exports.OAuth2Strategy = OAuth2Strategy;
    

    这意味着,“默认”(即策略)根本不是 oauth2...所以你最好明确地使用 OAuth2Strategy。它对我有用。我花了几个小时才发现这是问题所在......

    【讨论】:

      猜你喜欢
      • 2015-10-22
      • 2016-07-23
      • 2012-07-23
      • 1970-01-01
      • 2014-03-24
      • 1970-01-01
      • 2018-05-25
      • 1970-01-01
      • 2021-04-27
      相关资源
      最近更新 更多