Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
Session.get(sessionKey, function (err, sessionData) {
if (err) {
sails.log.error('Error retrieving session from socket.');
sessionData = {};
if (cb) cb(err);
return;
}
// Create session for first time if necessary
if (!util.isObject(sessionData)) {
sessionData = {};
}
// Otherwise session exists and everything is ok.
// Add method to trigger a save() of the session data
function SocketIOSession () {
this.save = function (cb) {
Session.set(sessionKey, req.session, function (err) {
if (err) {
sails.log.error('Error encountered saving session:');
sails.log.error(err);
}
if (cb) cb(err);
});
configure: function () {
// Validate config
// Ensure that secret is specified if a custom session store is used
if(sails.config.session) {
if(!util.isObject(sails.config.session)) {
throw new Error('Invalid custom session store configuration!\n' +
'\n' +
'Basic usage ::\n' +
'{ session: { adapter: "memory", secret: "someVerySecureString", /* ...if applicable: host, port, etc... */ } }' +
'\n\nCustom usage ::\n' +
'{ session: { store: { /* some custom connect session store instance */ }, secret: "someVerySecureString", /* ...custom settings.... */ } }'
);
}
}
// If session config is set, but secret is undefined, set a secure, one-time use secret
if(!sails.config.session || !sails.config.session.secret) {
sails.log.warn('Session secret must be identified!\n' +
'Should be of the form: `sails.config.session = { secret: "someVerySecureString" }`' +
'\nAutomatically generating one for now...' +
// If message name is not `message`, it's the verb!
if ( util.isString(messageName) && messageName.toLowerCase() !== 'message' ) {
return messageName.toUpperCase();
}
// try and parse the socket io data if it looks like JSON
var body;
if ( util.isString(socketIOData) ) {
try {
body = JSON.parse(socketIOData);
} catch(e) {}
}
// Only try to use the socket io data if it's usable
if ( util.isObject(body) ) {
if (util.isString(body.verb)) {
return body.verb.toUpperCase();
}
if (util.isString(body.method)) {
return body.method.toUpperCase();
}
}
return 'GET';
};
initialize: function (cb) {
var sessionConfig = sails.config.session;
// Intepret session adapter config and "new up" a session store
if (util.isObject(sessionConfig) && !util.isObject(sessionConfig.store)) {
switch (sessionConfig.adapter) {
// Session explicitly disabled
case null:
// Do nothing...
break;
// Supported session adapters
case 'memory':
sessionConfig.store = new(require('express').session.MemoryStore)();
break;
case 'redis':
sessionConfig.store = new(require('connect-redis')(require('express')))(sessionConfig);
break;
case 'mongo':
sessionConfig.store = new(require('connect-mongo')(require('express')))(sessionConfig);