2014-04-12 07:46:15 +04:00
|
|
|
var Store = require('express-session').Store,
|
2014-02-19 17:57:26 +04:00
|
|
|
models = require('./models'),
|
2013-11-24 18:29:36 +04:00
|
|
|
time12h = 12 * 60 * 60 * 1000,
|
2014-02-19 17:57:26 +04:00
|
|
|
|
|
|
|
BSStore;
|
2013-11-24 18:29:36 +04:00
|
|
|
|
|
|
|
// Initialize store and clean old sessions
|
2014-02-19 17:57:26 +04:00
|
|
|
BSStore = function BSStore(options) {
|
2013-11-24 18:29:36 +04:00
|
|
|
var self = this;
|
|
|
|
options = options || {};
|
|
|
|
Store.call(this, options);
|
|
|
|
|
2014-02-19 17:57:26 +04:00
|
|
|
models.Session.findAll()
|
2013-11-24 18:29:36 +04:00
|
|
|
.then(function (model) {
|
|
|
|
var i,
|
|
|
|
now = new Date().getTime();
|
|
|
|
for (i = 0; i < model.length; i = i + 1) {
|
|
|
|
if (now > model.at(i).get('expires')) {
|
|
|
|
self.destroy(model.at(i).get('id'));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
BSStore.prototype = new Store();
|
|
|
|
|
|
|
|
// store a given session
|
|
|
|
BSStore.prototype.set = function (sid, sessData, callback) {
|
|
|
|
var maxAge = sessData.cookie.maxAge,
|
|
|
|
now = new Date().getTime(),
|
|
|
|
expires = maxAge ? now + maxAge : now + time12h,
|
2014-02-19 17:57:26 +04:00
|
|
|
sessionModel = models.Session;
|
2013-11-24 18:29:36 +04:00
|
|
|
|
|
|
|
sessData = JSON.stringify(sessData);
|
|
|
|
|
|
|
|
//necessary since bookshelf updates models if id is set
|
|
|
|
sessionModel.forge({id: sid}).fetch()
|
|
|
|
.then(function (model) {
|
|
|
|
if (model) {
|
2014-01-18 23:14:38 +04:00
|
|
|
return sessionModel.forge({id: sid, expires: expires, sess: sessData }).save();
|
2013-11-24 18:29:36 +04:00
|
|
|
}
|
2014-01-18 23:14:38 +04:00
|
|
|
return sessionModel.forge({id: sid, expires: expires, sess: sessData })
|
|
|
|
.save(null, {method: 'insert'});
|
|
|
|
}).then(function () {
|
2013-11-24 18:29:36 +04:00
|
|
|
callback();
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
// fetch a session, if session is expired delete it
|
|
|
|
BSStore.prototype.get = function (sid, callback) {
|
|
|
|
var now = new Date().getTime(),
|
|
|
|
self = this,
|
|
|
|
sess,
|
|
|
|
expires;
|
|
|
|
|
2014-02-19 17:57:26 +04:00
|
|
|
models.Session.forge({id: sid})
|
2013-11-24 18:29:36 +04:00
|
|
|
.fetch()
|
|
|
|
.then(function (model) {
|
|
|
|
if (model) {
|
|
|
|
sess = JSON.parse(model.get('sess'));
|
|
|
|
expires = model.get('expires');
|
|
|
|
if (now < expires) {
|
|
|
|
callback(null, sess);
|
|
|
|
} else {
|
|
|
|
self.destroy(sid, callback);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
callback();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
// delete a given sessions
|
|
|
|
BSStore.prototype.destroy = function (sid, callback) {
|
2014-02-19 17:57:26 +04:00
|
|
|
models.Session.forge({id: sid})
|
2013-11-24 18:29:36 +04:00
|
|
|
.destroy()
|
|
|
|
.then(function () {
|
|
|
|
// check if callback is null
|
|
|
|
// session.regenerate doesn't provide callback
|
|
|
|
// cleanup at startup does neither
|
|
|
|
if (callback) {
|
|
|
|
callback();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
// get the count of all stored sessions
|
|
|
|
BSStore.prototype.length = function (callback) {
|
2014-02-19 17:57:26 +04:00
|
|
|
models.Session.findAll()
|
2013-11-24 18:29:36 +04:00
|
|
|
.then(function (model) {
|
|
|
|
callback(null, model.length);
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
// delete all sessions
|
|
|
|
BSStore.prototype.clear = function (callback) {
|
2014-02-19 17:57:26 +04:00
|
|
|
models.Session.destroyAll()
|
2013-11-24 18:29:36 +04:00
|
|
|
.then(function () {
|
|
|
|
callback();
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
module.exports = BSStore;
|