Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Required option added #11

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 12 additions & 5 deletions lib/session.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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();
Expand All @@ -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;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we could do this here instead of passing the options in for the callback.

if (self.options.required === true) { ...

var handshake = socket.handshake || {};
if (handshake.headers && handshake.headers.cookie) {
Expand All @@ -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();
}
}
};