-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathapp.js
64 lines (59 loc) · 1.94 KB
/
app.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
var app = require('express')(),
session = require('express-session'),
cookie = require('cookie'),
cookieParser = require('cookie-parser'),
sessionStore = new session.MemoryStore();
var COOKIE_SECRET = 'secret';
var COOKIE_NAME = 'sid';
app.set('views', __dirname + '/views');
app.set('view engine', 'ejs');
app.use(cookieParser(COOKIE_SECRET));
app.use(session({
name: COOKIE_NAME,
store: sessionStore,
secret: COOKIE_SECRET,
saveUninitialized: true,
resave: true,
cookie: {
path: '/',
httpOnly: true,
secure: false,
maxAge: null
}
}));
// HTTP session cookie is set here
// Must appear after session middleware
app.get('/', function (req, res) {
res.render('index');
});
var server = require('http').Server(app).listen(8888),
io = require('socket.io')(server);
io.use(function(socket, next) {
try {
var data = socket.handshake || socket.request;
if (! data.headers.cookie) {
return next(new Error('Missing cookie headers'));
}
console.log('cookie header ( %s )', JSON.stringify(data.headers.cookie));
var cookies = cookie.parse(data.headers.cookie);
console.log('cookies parsed ( %s )', JSON.stringify(cookies));
if (! cookies[COOKIE_NAME]) {
return next(new Error('Missing cookie ' + COOKIE_NAME));
}
var sid = cookieParser.signedCookie(cookies[COOKIE_NAME], COOKIE_SECRET);
if (! sid) {
return next(new Error('Cookie signature is not valid'));
}
console.log('session ID ( %s )', sid);
data.sid = sid;
sessionStore.get(sid, function(err, session) {
if (err) return next(err);
if (! session) return next(new Error('session not found'));
data.session = session;
next();
});
} catch (err) {
console.error(err.stack);
next(new Error('Internal server error'));
}
});