forked from outbounder/organic-express-mongo-sessions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
35 lines (31 loc) · 998 Bytes
/
index.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
var Session = require('express-session');
var MongoStore = require('connect-mongo')(Session);
var _ = require("underscore")
module.exports = function(plasma, dna) {
plasma.on(dna.reactOn, function(c){
var app = c.data || c[0].data
var sessionStore = new MongoStore(_.extend({db: dna.db}, dna["connect-mongo"]), function(e){
if(dna.emitReady)
plasma.emit({
type: dna.emitReady,
data: sessionStore,
session: session
})
})
var session = Session(_.extend({
secret: dna.cookie_secret,
store: sessionStore,
saveUninitialized: true,
resave: true
}, dna["express-session"]));
app.use(session);
plasma.on(dna.closeOn || "kill", function(c, next){
// close db connection
sessionStore.db.close(function(){
// workaround to notify express session that its sessionStore has closed db connection.
sessionStore.emit("disconnect")
next()
})
})
})
}