javascript - Passport-socketio 破坏了 Passport 的实现?

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

这是我的套接字配置:

// 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)

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

最佳答案

更新: 只需确保使用与 Passport-socketio 模块使用的相同版本的 Passport,目前这是版本 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)}

};

当然,只有当您像当前所做的那样传递自己的 Passport 对象时,这才有效。 这个问题与 Passport-socketio 需要 Passport 模块有关,该模块覆盖了 req.login 函数,明天我会仔细看看到底出了什么问题。

关于javascript - Passport-socketio 破坏了 Passport 的实现?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22432210/

相关文章:

javascript - 从 Await 调用中捕获错误后,如何停止在 Express.js 中执行?

node.js - Socket.io 命名空间中的用户数

r - 使用 R 连接到 socket.io 套接字

javascript - 使用node.js require加载模块

javascript - 如何在 Node Red 中同时运行多个查询?

javascript - 记录没有 new 的构造函数

node.js - 在高速公路 onclose 事件后重新连接

node.js - 如何 catch 与编剧的下载?

javascript - WebRTC:如何在提供和应答后添加流?

javascript - Firefox 中的 Primefaces 自动完成功能未关闭选择