护照套餐打破护照执行?

时间:2014-03-16 02:15:49

标签: 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.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方法留空,它不会崩溃,但也没有设置会话,我也没有登录。

2 个答案:

答案 0 :(得分:3)

<强>更新 只需确保使用护照socketio模块使用的相同版本的护照,目前这是0.2.0版本,您可能还需要升级护照-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)}

};

当然,这只有在您传递自己的护照对象时才会有效。 这个问题与护照socketio有关,需要护照模块覆盖req.login功能,明天我会好好看看到底出了什么问题。

答案 1 :(得分:0)

将护照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);
                }
            }));