我让它工作。我必须做的是访问 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