diff --git a/lib/session.js b/lib/session.js index 25279a8..0cb2150 100644 --- a/lib/session.js +++ b/lib/session.js @@ -20,8 +20,10 @@ function Session (options) { return new Session(options); } + options = options || {}; + function session (socket, cb) { - session.middleware(socket, cb); + session.middleware(socket, cb, options.required != undefined ? options.required : true); } session.__proto__ = Session.prototype; @@ -43,8 +45,6 @@ Session.init = function (session, options) { if (!(session instanceof Session)) return new Session(options); - options = options || {}; - debug('options', options); session.parser = options.parser || cookieParser(); @@ -62,7 +62,7 @@ Session.init = function (session, options) { * @param {functin} fn */ -Session.prototype.middleware = function (socket, cb) { +Session.prototype.middleware = function (socket, cb, required) { var self = this; var handshake = socket.handshake || {}; if (handshake.headers && handshake.headers.cookie) { @@ -79,7 +79,14 @@ Session.prototype.middleware = function (socket, cb) { }); } else { - cb(new Error('Missing Cookies')); + if (required === true) { + // Session required, send error + cb(new Error('Missing Cookies')); + } else { + // Session optional, set handshake.session to null + handshake.session = null; + cb(); + } } };