我使用Passport& amp;构建了一个在socket.io上中继的nodejs应用程序。用于身份验证和授权的passport.socketio,以及用于会话存储的connect-mongo的mongodb。
它在我的笔记本电脑上运行良好,但当我搬到云端(Azure-VM)时,我开始遇到奇怪的错误。
05-02-2014,11:47:06.500收听端口8081(https)
/home/azureuser/myapp/node_modules/mongodb/lib/mongodb/connection/base.js:242
throw message;
^
Error: Error in session store:
Error: failed to deserialize user out of session
at Object.io.set.passportSocketIo.authorize.fail (/home/azureuser/myapp/lib/express/socketio.js:25:23)
at /home/azureuser/myapp/node_modules/passport.socketio/lib/index.js:48:21
at /home/azureuser/myapp/node_modules/connect-mongo/lib/connect-mongo.js:229:23
at /home/azureuser/myapp/node_modules/mongodb/lib/mongodb/collection/query.js:147:5
at Cursor.nextObject (/home/azureuser/myapp/node_modules/mongodb/lib/mongodb/cursor.js:733:5)
at commandHandler (/home/azureuser/myapp/node_modules/mongodb/lib/mongodb/cursor.js:713:14)
at /home/azureuser/myapp/node_modules/mongodb/lib/mongodb/db.js:1806:9
at Server.Base._callHandler (/home/azureuser/myapp/node_modules/mongodb/lib/mongodb/connection/base.js:442:41)
at /home/azureuser/myapp/node_modules/mongodb/lib/mongodb/connection/server.js:485:18
at MongoReply.parseBody (/home/azureuser/myapp/node_modules/mongodb/lib/mongodb/responses/mongo_reply.js:68:5)
任何想法从哪里开始?
答案 0 :(得分:3)
好吧我找到了,passport.socketio取决于Passport,并拥有自己的护照“版本”。 这意味着在我设置serializeUser / deserializeUser的代码中,它只影响我用于REST的通行证:
passport.serializeUser(function(user, done) {
done(null, user);
});
passport.deserializeUser(function(id, done) {
done(null, id);
});
默认情况下,passport.socketio执行:
var defaults = {
passport: require('passport'),
key: 'connect.sid',
secret: null,
...
};
意味着不使用serializeUser / deserializeUser,这反过来导致:
Error: failed to deserialize user out of session
解决方案非常简单,只需将用于REST的护照传递给passport.socketio
即可io.set('authorization', passportSocketIo.authorize({
passport : passport,
cookieParser: express.cookieParser,
...
}));