问题描述
这是我的套接字配置:
// 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);
}
}));
这是login的passport实现,在没有passport.socketio的情况下效果很好:
This is the passport implementation of login, which works great without 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);
});
}
这是我收到的错误,我不知道如何解决:
This is the error I receive, and I have no idea how to solve it:
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 方法留空,它不会崩溃,但也不会设置会话并且我没有登录.
Why does it want to run some kind of function on the user object? I dont understand! If I leave the done method empty it won't crash but also no session is set and I am not logged in.
推荐答案
更新:只需确保使用与passport-socketio 模块使用相同版本的passport,目前这是0.2.0 版,您可能还需要升级您的passport-facebook 模块
update:just make sure to use the same version of passport as the passport-socketio module is using, currently this is version 0.2.0, you might have to upgrade your passport-facebook module as well
旧帖子:
我遇到了同样的问题并通过更改passport-socket.io index.js文件解决了这个问题:
I had the same problem and solved this by changing the passport-socket.io index.js file:
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 函数的护照模块有关的passport-socketio有关,我明天会更好地看看到底出了什么问题.
Of course, this will only work if you pass in your own passport object like you are currently doing.The problem has something to do with passport-socketio requiring the passport module which overwrites the req.login function, I'll have a better look tomorrow to figure out exactly what is going wrong.
这篇关于护照socketio打破护照实施?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!