-
Notifications
You must be signed in to change notification settings - Fork 15
/
mongoProvider.js
37 lines (37 loc) · 1.17 KB
/
mongoProvider.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
var MongoClient = require('mongodb').MongoClient;
module.exports = {
init: function(expressConfig, session, cb) {
var self = this;
var MongoStore = require('connect-mongo')(session);
MongoClient.connect(expressConfig.config.url, function(err, mongo) {
if (err) {
return cb(err);
}
self.store = new MongoStore({
db: mongo,
ttl: expressConfig.config.ttl || 14 * 24 * 60 * 60 // = 14 days. Default
});
self.db = mongo;
return cb(null);
});
},
verifySession: function(token, cb) {
//For a given session token, there should only be a maximum of 1
this.db.collection('sessions').findOne({
'_id': token
}, function(err, foundSession) {
//For the session to be valid, the expiry date must be greater than now.
var valid = !err && foundSession && new Date() < new Date(foundSession.expires);
foundSession.isValid = valid;
return cb(err, foundSession);
});
},
revokeSession: function(token, cb) {
this.db.collection('sessions').deleteOne({
'_id': token
}, cb);
},
defaultConfig: {
url: 'mongodb://localhost:27017/raincatcher-demo-auth-session-store'
}
};