【问题标题】:passport-socketio breaks passport implementation?护照-socketio 打破护照实施?
【发布时间】:2014-04-21 08:24:39
【问题描述】:

这是我的套接字配置:

// set authorization for socket.io
    io.set('authorization', passportSocketIo.authorize({
        cookieParser: express.cookieParser,
        key:         'connect.sid',       // the name of the cookie where express/connect stores its session_id
        secret:      '1234',    // the session_secret to parse the cookie
        store:       sessionStore,        // we NEED to use a sessionstore. no memorystore please
        success: function (data, accept) {
            console.log('successful connection to socket.io');

            // The accept-callback still allows us to decide whether to
            // accept the connection or not.
            accept(null, true);
        },
        fail: function (data, message, error, accept) {
            if(error)
                throw new Error(message);
            console.log('failed connection to socket.io:', message);

            // We use this callback to log all of our failed connections.
            accept(null, false);
        }
    }));

这是登录的passport实现,不用passport.socketio也很好用:

exports.facebookStrategy = function () {

    return new FacebookStrategy({
        //client configuration
    },
    function(accessToken, refreshToken, profile, done) {
        User.findOrCreate({uid: profile.id},
            {
                username: profile.username,
                gender: profile.gender,
                first_name: profile.name.givenName,
                last_name: profile.name.familyName,
                photo: 'https://graph.facebook.com/' + profile.username + '/picture?type=large',
                access_token: accessToken
            },
            function(err, user) {
                if (err) { return done(err); }
                done(null, user); // This is were it breaks
        });
        console.log(profile);
    });

}

这是我收到的错误,我不知道如何解决它:

TypeError: object is not a function
    at pass (node_modules/passport/lib/passport/index.js:287:14)
    at Passport.serializeUser (/node_modules/passport/lib/passport/index.js:289:5)
    at IncomingMessage.req.login.req.logIn (node_modules/passport.socketio/node_modules/passport/lib/http/request.js:48:29)
    at Context.delegate.success (node_modules/passport/lib/passport/middleware/authenticate.js:194:13)
    at Context.actions.success (node_modules/passport/lib/passport/context/http/actions.js:21:25)
    at verified (/sociable/node_modules/passport-facebook/node_modules/passport-oauth2/lib/strategy.js:181:18)
    at /app/lib/auth.js:25:5
    at Promise.<anonymous> (/node_modules/mongoose-findorcreate/index.js:31:11)
    at Promise.<anonymous> (node_modules/mongoose/node_modules/mpromise/lib/promise.js:177:8)
    at Promise.EventEmitter.emit (events.js:95:17)

为什么要在用户对象上运行某种函数?我不明白!如果我将 done 方法留空,它不会崩溃,但也没有设置会话并且我没有登录。

【问题讨论】:

    标签: javascript node.js socket.io passport.js passport.socketio


    【解决方案1】:

    更新: 只需确保使用与 passport-socketio 模块使用的相同版本的护照,目前是 0.2.0 版,您可能还需要升级您的 passport-facebook 模块

    旧帖:

    我遇到了同样的问题,通过更改 passport-socket.io index.js 文件解决了这个问题:

    var defaults = {
    passport:     require('passport'),
    key:          'connect.sid',
    secret:       null,
    store:        null,
    success:      function(data, accept){accept(null, true)},
    fail:         function(data, message, critical, accept){accept(null, false)}
    

    };

    将此默认配置更改为:

    var defaults = {
    passport:     null,
    key:          'connect.sid',
    secret:       null,
    store:        null,
    success:      function(data, accept){accept(null, true)},
    fail:         function(data, message, critical, accept){accept(null, false)}
    

    };

    当然,这只有在您像当前一样传入自己的护照对象时才有效。 问题与需要覆盖 req.login 功能的护照模块有关的护照套接字有关,我明天会更好地了解到底出了什么问题。

    【讨论】:

      【解决方案2】:

      将passport var 传递给passport-socketio 对象将解决这个问题。

       io.set('authorization', passportSocketIo.authorize({
                      passport: passport, //use passport
                      cookieParser: express.cookieParser,
                      key:         'connect.sid',       // the name of the cookie where express/connect stores its session_id
                      secret:      '1234',    // the session_secret to parse the cookie
                      store:       sessionStore,        // we NEED to use a sessionstore. no memorystore please
                      success: function (data, accept) {
                          console.log('successful connection to socket.io');
      
                          // The accept-callback still allows us to decide whether to
                          // accept the connection or not.
                          accept(null, true);
                      },
                      fail: function (data, message, error, accept) {
                          if(error)
                              throw new Error(message);
                          console.log('failed connection to socket.io:', message);
      
                          // We use this callback to log all of our failed connections.
                          accept(null, false);
                      }
                  }));
      

      【讨论】:

      • 其实并没有解决问题。为什么它要解决问题?你能详细说明一下吗?我仍然收到同样的错误。提前致谢。
      • 实际上,passport-socketio 使用旧版本的护照,如果我们在它的配置中设置了护照,那么 passport-socketio 会自动设置依赖关系。
      • 它默认为 require('passport') 那么为什么要使用旧版本的护照呢?无论如何,当我尝试这样做时,仍然存在相同的错误消息。
      猜你喜欢
      • 1970-01-01
      • 2023-02-24
      • 2017-10-14
      • 2019-01-06
      • 2019-06-23
      • 1970-01-01
      • 2012-09-13
      • 2017-07-27
      • 1970-01-01
      相关资源
      最近更新 更多