Socket
Socket
Sign inDemoInstall

connect-mysql-passport

Package Overview
Dependencies
Maintainers
1
Versions
2
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

connect-mysql-passport - npm Package Compare versions

Comparing version 0.1.0 to 0.2.0

.idea/.name

132

lib/connect-mysql.js

@@ -0,37 +1,68 @@

'use strict';
var util = require('util');
module.exports = function (connect) {
var Store = connect.session.Store;
var
MySQLStore,
DELETE_FREQ = 20;
function MySQLStore(options) {
var self = this;
self.cleanup = true;
self.sqlCleanup = true;
Store.call(self, options);
// cleanup at random
/**
* MySQLStore
* extend/override connect.session.store
*
* @constructor
* @method MySQLStore
*/
MySQLStore = function (options) {
var _this = this;
_this.isCleanup = true;
_this.isSqlCleanup = true;
connect.session.Store.call(_this, options);
// option: cleanup at random
if (options.hasOwnProperty('cleanup')) {
self.cleanup = options.cleanup;
this.isCleanup = options.cleanup;
}
// cleanup by SQL event
// option: cleanup by SQL event
if (options.hasOwnProperty('sqlCleanup')) {
self.cleanup = false;
self.sqlCleanup = options.sqlCleanup;
_this.isCleanup = false;
_this.isSqlCleanup = options.sqlCleanup;
}
self.mysql = options.client;
self.mysql.query('CREATE TABLE IF NOT EXISTS `Session` (`sid` varchar(255) NOT NULL, `session` text NOT NULL, `expires` int(11) DEFAULT NULL, `user` int(11) DEFAULT NULL, `created` timestamp NULL DEFAULT CURRENT_TIMESTAMP, PRIMARY KEY (`sid`), KEY `idx_se_expires` (`expires`), KEY `idx_se_user` (`user`))',
function dbCreated(err) {
if (err) throw err;
// option: delete freq
if(options.freq && !isNaN(options.freq)) {
DELETE_FREQ = options.freq;
}
_this.mysql = options.client;
_this.mysql.query('CREATE TABLE IF NOT EXISTS `Session` (`sid` varchar(255) NOT NULL, `session` text NOT NULL, `expires` int(11) DEFAULT NULL, `user` int(11) DEFAULT NULL, `created` timestamp NULL DEFAULT CURRENT_TIMESTAMP, PRIMARY KEY (`sid`), KEY `idx_se_expires` (`expires`), KEY `idx_se_user` (`user`))', function (err) {
if (err) {
throw err;
}
// cleanup by SQL event
if (self.sqlCleanup) {
self.mysql.query('CREATE EVENT IF NOT EXISTS `sess_cleanup` ON SCHEDULE EVERY 15 MINUTE DO DELETE FROM `Session` WHERE `expires` < UNIX_TIMESTAMP()');
self.mysql.query('SET GLOBAL event_scheduler = 1');
if (_this.isSqlCleanup) {
_this.mysql.query('CREATE EVENT IF NOT EXISTS `sess_cleanup` ON SCHEDULE EVERY 15 MINUTE DO DELETE FROM `Session` WHERE `expires` < UNIX_TIMESTAMP()');
_this.mysql.query('SET GLOBAL event_scheduler = 1');
}
});
}
};
function cleanup() {
self.mysql.query('DELETE FROM `Session` WHERE `expires` < UNIX_TIMESTAMP()', function dbCleaned(err) {
// Inherit Store from the connect.session.store
util.inherits(MySQLStore, connect.session.Store);
/**
* Cleanup
* delete expires sessions
*
* @method cleanup
*/
MySQLStore.prototype.cleanup = function () {
this.mysql.query('DELETE FROM `Session` WHERE `expires` < UNIX_TIMESTAMP()', function (err) {
if (err) {

@@ -41,7 +72,20 @@ throw err;

});
}
};
MySQLStore.prototype.__proto__ = Store.prototype;
/**
* Get
* override connect session store function
*
* @method get
* @param {String} sid
* @param {Function} callback
*/
MySQLStore.prototype.get = function (sid, callback) {
// cleanup at random
if (this.isCleanup && !this.isSqlCleanup && (Math.floor(Math.random() * (DELETE_FREQ + 1)) === DELETE_FREQ)) {
this.cleanup();
}
this.mysql.query('SELECT `session` FROM `Session` WHERE `sid` = ?', [sid],function (err, result) {

@@ -54,6 +98,18 @@ if (result && result[0] && result[0].session) {

}).on('error', function (err) {
callback(err);
if (typeof callback === 'function') {
callback(err);
}
});
};
/**
* Get
* override connect session store function
*
* @method get
* @param {String} sid
* @param {Object} session
* @param {Function} callback
*/
MySQLStore.prototype.set = function (sid, session, callback) {

@@ -63,7 +119,2 @@ var expires = new Date(session.cookie.expires).getTime() / 1000,

// cleanup at random
if (this.cleanup && Math.floor(Math.random() * 6) === 5) {
cleanup();
}
if (session.passport.user) {

@@ -75,10 +126,21 @@ user = session.passport.user;

this.mysql.query('INSERT INTO `Session` (`sid`, `session`, `expires`, `user`) VALUES(?, ?, ?, ?) ON DUPLICATE KEY UPDATE `session` = ?, `expires` = ?, `user` = ?',
[sid, session, expires, user, session, expires, user], function sessionInserted(err) {
callback(err);
});
[sid, session, expires, user, session, expires, user], function (err) {
if (typeof callback === 'function') {
callback(err);
}
});
};
/**
* Destroy
* override connect session store function
*
* @method destroy
* @param {String} sid
* @param {Function} callback
*/
MySQLStore.prototype.destroy = function (sid, callback) {
this.mysql.query('DELETE FROM `Session` WHERE `sid` = ?', [sid], function sessionDestroyed(err) {
if(callback) {
this.mysql.query('DELETE FROM `Session` WHERE `sid` = ?', [sid], function (err) {
if (typeof callback === 'function') {
callback(err);

@@ -85,0 +147,0 @@ }

@@ -6,3 +6,3 @@ {

"readme": "This package extends the https://github.com/nlf/connect-mysql module. \n\n What's plus?\n - add created and user field to the session table \n - can delete expired sessions without SQL event",
"version": "0.1.0",
"version": "0.2.0",
"repository": {

@@ -9,0 +9,0 @@ "type": "git",

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc