-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
41 lines (37 loc) · 1.35 KB
/
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
36
37
38
39
40
41
var Expirer = require('expire-unused-keys')
var UUID = require('random-uuid-v4')
function getFullApi(jlc, sessionId) {
return {
beginAuthentication: jlc.beginAuthentication.bind(jlc, sessionId),
isAuthenticated: jlc.isAuthenticated.bind(jlc, sessionId),
unauthenticate: jlc.unauthenticate.bind(jlc, sessionId)
}
}
function createSession(jlc, expirer, cb) { //cb(err, api, sessionId)
var sessionId = UUID()
expirer.touch(sessionId)
cb(null, getFullApi(jlc, sessionId), sessionId)
}
function continueSession(jlc, expirer, sessionDb, sessionId, cb) { //cb(err, api, sessionId)
sessionDb.get(sessionId, function (err, authed) {
if (err && err.notFound) {
cb(new Error("Invalid Session Id"))
} else if (err) {
cb(err)
} else {
expirer.touch(sessionId)
cb(null, getFullApi(jlc, sessionId), sessionId)
}
})
}
module.exports = function sessionManager(justLoginCore, sessionDb, opts) { //Exposed to the browser via dnode
opts = opts || {}
var expirer = new Expirer(opts.timeoutMs || 86400000, sessionDb, opts.checkIntervalMs || 1000) //hey this needs a REAL db
expirer.on('expire', function (key) {
sessionDb.del(key, function () {})
})
return {
createSession: createSession.bind(null, justLoginCore, expirer),
continueSession: continueSession.bind(null, justLoginCore, expirer, sessionDb)
}
}