这是我的套接字配置:
// 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/