2021-01-21 21:06:52 +00:00
|
|
|
'use strict';
|
2019-02-08 23:20:57 +01:00
|
|
|
/*
|
2013-02-13 01:33:22 +00:00
|
|
|
* Stores session data in the database
|
|
|
|
* Source; https://github.com/edy-b/SciFlowWriter/blob/develop/available_plugins/ep_sciflowwriter/db/DirtyStore.js
|
|
|
|
* This is not used for authors that are created via the API at current
|
2019-01-23 16:58:43 +00:00
|
|
|
*
|
|
|
|
* RPB: this module was not migrated to Promises, because it is only used via
|
|
|
|
* express-session, which can't actually use promises anyway.
|
2013-02-13 01:33:22 +00:00
|
|
|
*/
|
|
|
|
|
2021-01-21 21:06:52 +00:00
|
|
|
const DB = require('./DB');
|
|
|
|
const Store = require('express-session').Store;
|
|
|
|
const log4js = require('log4js');
|
2013-02-13 01:33:22 +00:00
|
|
|
|
2020-09-21 16:57:10 -04:00
|
|
|
const logger = log4js.getLogger('SessionStore');
|
2020-09-21 16:45:25 -04:00
|
|
|
|
2020-09-21 17:05:29 -04:00
|
|
|
module.exports = class SessionStore extends Store {
|
|
|
|
get(sid, fn) {
|
2020-11-23 13:24:19 -05:00
|
|
|
logger.debug(`GET ${sid}`);
|
|
|
|
DB.db.get(`sessionstorage:${sid}`, (err, sess) => {
|
2020-09-21 17:05:29 -04:00
|
|
|
if (sess) {
|
2020-11-23 13:24:19 -05:00
|
|
|
sess.cookie.expires = ('string' === typeof sess.cookie.expires
|
|
|
|
? new Date(sess.cookie.expires) : sess.cookie.expires);
|
2020-09-21 17:05:29 -04:00
|
|
|
if (!sess.cookie.expires || new Date() < sess.cookie.expires) {
|
|
|
|
fn(null, sess);
|
|
|
|
} else {
|
|
|
|
this.destroy(sid, fn);
|
|
|
|
}
|
2013-02-13 01:33:22 +00:00
|
|
|
} else {
|
2020-09-21 17:05:29 -04:00
|
|
|
fn();
|
2013-02-13 01:33:22 +00:00
|
|
|
}
|
2020-09-21 17:05:29 -04:00
|
|
|
});
|
|
|
|
}
|
2013-02-13 01:33:22 +00:00
|
|
|
|
2020-09-21 17:05:29 -04:00
|
|
|
set(sid, sess, fn) {
|
2020-11-23 13:24:19 -05:00
|
|
|
logger.debug(`SET ${sid}`);
|
|
|
|
DB.db.set(`sessionstorage:${sid}`, sess, fn);
|
2020-09-21 17:05:29 -04:00
|
|
|
}
|
2013-02-13 01:33:22 +00:00
|
|
|
|
2020-09-21 17:05:29 -04:00
|
|
|
destroy(sid, fn) {
|
2020-11-23 13:24:19 -05:00
|
|
|
logger.debug(`DESTROY ${sid}`);
|
|
|
|
DB.db.remove(`sessionstorage:${sid}`, fn);
|
2020-09-21 17:05:29 -04:00
|
|
|
}
|
2013-02-13 01:33:22 +00:00
|
|
|
};
|