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

do not initialize session if uneeded. helps express-session not create ... #320

Merged
merged 1 commit into from
Aug 20, 2015
Merged
Show file tree
Hide file tree
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
7 changes: 7 additions & 0 deletions lib/http/request.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,14 @@ req.logIn = function(user, options, done) {
var self = this;
this._passport.instance.serializeUser(user, this, function(err, obj) {
if (err) { self[property] = null; return done(err); }
if(!self._passport.session) {
self._passport.session = {};
}
self._passport.session.user = obj;
if(!self.session) {
self.session = {};
}
self.session[self._passport.instance._key] = self._passport.session;
done();
});
} else {
Expand Down
9 changes: 1 addition & 8 deletions lib/middleware/initialize.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,15 +50,8 @@ module.exports = function initialize(passport) {
if (req.session && req.session[passport._key]) {
// load data from existing session
req._passport.session = req.session[passport._key];
} else if (req.session) {
// initialize new session
req.session[passport._key] = {};
req._passport.session = req.session[passport._key];
} else {
// no session is available
req._passport.session = {};
}

next();
};
};
7 changes: 5 additions & 2 deletions lib/strategies/session.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,11 @@ SessionStrategy.prototype.authenticate = function(req, options) {
if (!req._passport) { return this.error(new Error('passport.initialize() middleware not in use')); }
options = options || {};

var self = this
, su = req._passport.session.user;
var self = this,
su;
if(req._passport.session)
su = req._passport.session.user;

if (su || su === 0) {
// NOTE: Stream pausing is desirable in the case where later middleware is
// listening for events emitted from request. For discussion on the
Expand Down
26 changes: 24 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,28 @@
"node": ">= 0.4.0"
},
"scripts": {
"test": "node_modules/.bin/mocha --reporter spec --require test/bootstrap/node test/*.test.js test/**/*.test.js"
}
"test": "mocha --reporter spec --require test/bootstrap/node test/*.test.js test/**/*.test.js"
},
"gitHead": "4dce9d99a009fdec8a6b83a8cbc99d119a73c561",
"_id": "[email protected]",
"_shasum": "a7d34c07b30fb605be885edbc8c93e5142e38574",
"_from": "passport@^0.2.1",
"_npmVersion": "1.4.23",
"_npmUser": {
"name": "jaredhanson",
"email": "[email protected]"
},
"maintainers": [
{
"name": "jaredhanson",
"email": "[email protected]"
}
],
"dist": {
"shasum": "a7d34c07b30fb605be885edbc8c93e5142e38574",
"tarball": "http://registry.npmjs.org/passport/-/passport-0.2.1.tgz"
},
"directories": {},
"_resolved": "https://registry.npmjs.org/passport/-/passport-0.2.1.tgz",
"readme": "ERROR: No README data found!"
}
14 changes: 5 additions & 9 deletions test/authenticator.middleware.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,8 @@ describe('Authenticator', function() {
expect(passport._userProperty).to.equal('user');
});

it('should initialize namespace within session', function() {
expect(request.session.passport).to.be.an('object');
expect(Object.keys(request.session.passport)).to.have.length(0);
it('should not initialize namespace within session', function() {
expect(request.session.passport).to.be.undefined;
});

it('should expose authenticator on internal request property', function() {
Expand All @@ -51,8 +50,7 @@ describe('Authenticator', function() {
});

it('should expose session storage on internal request property', function() {
expect(request._passport.session).to.be.an('object');
expect(Object.keys(request._passport.session)).to.have.length(0);
expect(request._passport.session).to.be.undefined;
});
});

Expand Down Expand Up @@ -82,8 +80,7 @@ describe('Authenticator', function() {
});

it('should initialize namespace within session', function() {
expect(request.session.passport).to.be.an('object');
expect(Object.keys(request.session.passport)).to.have.length(0);
expect(request.session.passport).to.be.undefined;
});

it('should expose authenticator on internal request property', function() {
Expand All @@ -93,8 +90,7 @@ describe('Authenticator', function() {
});

it('should expose session storage on internal request property', function() {
expect(request._passport.session).to.be.an('object');
expect(Object.keys(request._passport.session)).to.have.length(0);
expect(request._passport.session).to.be.undefined;
});
});

Expand Down
9 changes: 3 additions & 6 deletions test/middleware/initialize.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,7 @@ describe('middleware/initialize', function() {
});

it('should expose empty object as session storage on internal request property', function() {
expect(request._passport.session).to.be.an('object');
expect(Object.keys(request._passport.session)).to.have.length(0);
expect(request._passport.session).to.be.undefined;
});
});

Expand All @@ -67,8 +66,7 @@ describe('middleware/initialize', function() {
});

it('should initialize namespace within session', function() {
expect(request.session.passport).to.be.an('object');
expect(Object.keys(request.session.passport)).to.have.length(0);
expect(request.session.passport).to.be.undefined;
});

it('should expose authenticator on internal request property', function() {
Expand All @@ -78,8 +76,7 @@ describe('middleware/initialize', function() {
});

it('should expose session storage on internal request property', function() {
expect(request._passport.session).to.be.an('object');
expect(Object.keys(request._passport.session)).to.have.length(0);
expect(request._passport.session).to.be.undefined;
});
});

Expand Down