【问题标题】:passport google oauth on localhost本地主机上的护照谷歌oauth
【发布时间】:2014-08-12 17:29:40
【问题描述】:

我在使用通行证进行节点身份验证方面非常陌生,因此有很多代码 sn-ps

我的服务器配置为:

var router = require('./app/config/routes');
var googleStrategy = require('./app/config/passport');
var session = require("express-session");

var passport = require('passport');
app.use(session({secret : '<secret-key>'}));
app.use(passport.initialize());
app.use(passport.session());
googleStrategy(passport); 

我的路线配置为

module.exports = function(app, passport) {

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

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

    .... ALSO configured /profile and /fail
};

我的护照配置为

passport.serializeUser(function(user, callback){
        console.log('serializing user.');
        callback(null, user);
    });

    passport.deserializeUser(function(user, callback){
       console.log('deserialize user.');
       callback(null, user);
    });

    var processRequest = function(token, refreshToken, profile, callback){
        process.nextTick(function(){
           console.log('id : '+ profile.id);
           console.log('name :'+ profile.displayName);
           console.log('email :' + profile.emails);
           console.log('token : '+ token);
        });
    };

    passport.use(new GoogleStrategy({
        clientID: 'client ID',
        clientSecret : 'client SECRET',
        callbackURL : 'http://127.0.0.1:8080/auth/google/callback',
        realm : 'http://127.0.0.1:8080'
    }, processRequest));

问题:在转到 /auth/google 时,我从来没有得到确认屏幕。我应该看什么?

更新:

将路由更改为如下所示的配置使其工作。

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

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

【问题讨论】:

    标签: node.js express oauth-2.0 passport.js


    【解决方案1】:

    我同意@Seiya,但我会添加重定向

    app.get(
        "/auth/google/callback", 
        passport.authenticate('google'),
        (req, res) => {
          res.redirect('/whatever')
        }
    );
    

    【讨论】:

      【解决方案2】:

      目前OAUTH2协议的身份验证和授权被google很好地支持。所以最好使用相同的。 Here is google 的文档。使用 'passport-google-oauth' 模块。这是实现。这应该是应用程序对象配置,还可以看到从 passport-google-oauth 模块中使用了 oauth2strategy 对象,还可以查看 app.get 路由注册中的范围。

      var googleStrategy = require('passport-google-oauth').OAuth2Strategy;
        app.configure(function() {
      
          app.set('views',  './views');
          app.set('view engine', 'jade');
          app.use(express.favicon());
          app.use(express.logger('dev'));
          app.use(express.cookieParser());
          app.use(express.bodyParser());
          app.use(express.session({secret:'MySecret'}));
          app.use(passport.initialize());
          app.use(passport.session());
          app.use(express.methodOverride());
          app.use(app.router);
          app.use(express.static('./public'));
      });
      
      app.get('/auth/google', select.passport.authenticate('google',{scope: 'https://www.googleapis.com/auth/plus.me https://www.google.com/m8/feeds https://www.googleapis.com/auth/userinfo.email https://www.googleapis.com/auth/userinfo.profile'}));
      
      app.get('/auth/google/callback', function() {
          passport.authenticate('google', {
              successRedirect: '/profile',
              failureRedirect: '/fail'
          });
      });
      app.get('/logout', function (req, res) {
              req.logOut();
              res.redirect('/');
          });
      

      但在创建新策略之前,请转到谷歌开发者控制台并获取 clientID 和 secret 。以下是步骤

      1. 去这个link 并创建项目,这里是同一个 的快照
      2. 给一个新的项目名称和ID,这是快照
      3. 创建新项目大约需要一分钟,一旦创建新项目,它会将您重定向到应用程序的应用程序配置。在重定向页面中选择 APIS AND AUTH -> API's ,在 API 页面中启用 GOogle+ API ,这是它的快照
      4. 然后转到凭据(在 API 下方),然后单击 Create New Client Id,并为您的应用注册域和回调(将域配置为 localhost),这是它的快照! 5.然后你会得到你的新ID和秘密。使用它们来创建新的策略

        passport.use(new googleStrategy({
            clientID: '<TheNewclientID>',
            clientSecret: '<The New Secret>',
        
            callbackURL: "http://locahost:8080/auth/google/callback"
        },
        function (accessToken, refreshToken, profile, done) {
            console.log(profile); //profile contains all the personal data returned 
            done(null, profile)
        }
        ));
        

      6.现在序列化和反序列化

      passport.serializeUser(function(user, callback){
              console.log('serializing user.');
              callback(null, user.id);
          });
      
      passport.deserializeUser(function(user, callback){
             console.log('deserialize user.');
             callback(null, user.id);
          });
      

      运行服务器并转到 localhost:8080/auth/google (不要使用 127.0.0.1:8080 而不是 locahost )。这应该让它工作:)

      [其他有用的链接: 查看 kvcrawford 在this 页面中对模块回购的第一条评论 Passport-google 是另一个流行的模块,用于使用 google 提供登录,它现在有点过时了,这里是 link 关于它最近的问题]

      【讨论】:

      • 感谢您的详尽回答。我解决了我的问题,即设置更新中显示的路线。但最好从第二个来源验证所有内容:)
      • 需要注意的是,在使用 'localhost' 作为域时,必须删除 'www',例如:'localhost:3000'
      • 看起来 google 不允许 localhost url 进行重定向:“redirect_uri 的参数值无效:不允许非公共域:locahost:4000/auth/google/callback
      • @Mnebuerquo 它对我有用 http://127.0.0.1/auth/google/callback 你错过了 http:// 吗?
      【解决方案3】:

      在网络上的大多数示例中,路由代码是这样完成的:

      app.get('/auth/google', passport.authenticate('google'));
      

      根据Express Referenceapp.get方法的回调被赋予了三个参数,requestresponse和'next'。也就是说,上例中的 authenticate 方法返回一个函数对象,并使用三个参数 requestresponse 和 'next' 执行。

      所以,如果你想像这样在app.get 方法的回调函数中进行身份验证:

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

      那么你应该写:

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

      【讨论】:

        猜你喜欢
        • 2019-01-29
        • 1970-01-01
        • 2014-10-20
        • 2020-11-27
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-08-05
        相关资源
        最近更新 更多