私はそれを働かせました。私がしなければならなかったのは、sessionStore にアクセスすることです。他の誰かがこの特定の問題に遭遇した場合のコードは次のとおりです。
// # app.js
var express = require('express'),
routes = require('./routes'),
http = require('http'),
path = require('path'),
app = express(),
passport = require('passport'),
SessionMongoose = require("session-mongoose"),
mongooseSessionStore = new SessionMongoose({
url: "mongodb://localhost/login",
interval: 120000
});
var config = require('game/config.js'), // holds my whole server configuration
server = require('game/lib/server.js');
// save sessionStore to config for later access
config.setSessionStore(mongooseSessionStore);
// configure express to use passport and mongooseSessionStore
app.configure(function(){
app.set('port', config.port);
app.set('env', config.environment);
app.set('dbPrefix', config.dbPrefix);
app.set('views', __dirname + '/views');
app.set('view engine', 'jade');
app.use(express.favicon());
app.use(express.bodyParser());
app.use(express.cookieParser());
app.use(express.session({secret : 'totallysecret', store : mongooseSessionStore })),
app.use(express.methodOverride());
app.use(passport.initialize());
app.use(passport.session());
app.use(app.router);
app.use(express['static'](path.join(__dirname, 'public')));
});
app.get('/', routes.index);
app.get('/auth/facebook', passport.authenticate('facebook', { scope: 'email' }));
app.get('/auth/facebook/callback',
passport.authenticate('facebook', { successRedirect: '/',
failureRedirect: '/' }));
// #### Init httpserver
var httpServer = http.createServer(app);
httpServer.listen(app.get('port'));
// #### Server startup
server.init(httpServer);
私のシリアライゼーション関数は次のように単純に見えます:
passport.serializeUser(function(user, done) {
// saves user.email to session.passport.user
done(null, user.email);
});
passport.deserializeUser(function(obj, done) {
done(null, obj);
});
そして最後に socket.io の実装:
var util = require('util'),
connect = require('connect'),
parseSignedCookie = connect.utils.parseSignedCookie,
cookie = require('express/node_modules/cookie'),
io = require('socket.io').listen(httpServer);
var config = require('../config.js');
io.configure(function () {
io.set('authorization', function (data, callback) {
if(data.headers.cookie) {
// save parsedSessionId to handshakeData
data.cookie = cookie.parse(data.headers.cookie);
data.sessionId = parseSignedCookie(data.cookie['connect.sid'], 'totallysecret');
}
callback(null, true);
});
io.on('connection', function(socket) {
// reference to my initialized sessionStore in app.js
var sessionStore = config.sessionStore;
var sessionId = socket.handshake.sessionId;
sessionStore.get(sessionId, function(err, session) {
if( ! err) {
if(session.passport.user) {
console.log('This is the users email address %s', session.passport.user);
}
});
});
});
アクセスできる session-mongoose モジュールを使用します。
sessionStore.get(sessionId, callback)
sessionStore.set(sessionId, data, callback)
sessionStore.destroy(sessionId, callback)
sessionStore.all(callback) // returns all available sessions
sessionStore.clear(callback) // deletes all session data
sessionStore.length(callback) // returns number of sessions in the