-
Notifications
You must be signed in to change notification settings - Fork 30
/
Copy pathindex.js
63 lines (48 loc) · 1.73 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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
const Store = require('./libs/store.js');
module.exports = (opts = {}) => {
const { key = 'koa:sess', store = new Store() } = opts;
return async (ctx, next) => {
let id = ctx.cookies.get(key, opts);
let need_refresh = false;
if (!id) {
ctx.session = {};
} else {
ctx.session = await store.get(id, ctx);
// reassigning session ID if current is not found
if (ctx.session == null) {
id = await store.getID(24);
need_refresh = true;
}
// check session must be a no-null object
if (typeof ctx.session !== 'object' || ctx.session == null) {
ctx.session = {};
}
}
const old = JSON.stringify(ctx.session);
// add refresh function
ctx.session.refresh = () => {need_refresh = true}
await next();
// remove refresh function
if (ctx.session && 'refresh' in ctx.session) {
delete ctx.session.refresh
}
const sess = JSON.stringify(ctx.session);
// if not changed
if (!need_refresh && old == sess) return;
// if is an empty object
if (sess == '{}') {
ctx.session = null;
}
// need clear old session
if (id && !ctx.session) {
await store.destroy(id, ctx);
ctx.cookies.set(key, null);
return;
}
// set/update session
const sid = await store.set(ctx.session, Object.assign({}, opts, {sid: id}), ctx);
if (!id || id !== sid || need_refresh) ctx.cookies.set(key, sid, opts);
}
}
// Reeexport Store to not use reference to internal files
module.exports.Store = Store;