Skip to content

Commit

Permalink
Refactor internal passport property on request.
Browse files Browse the repository at this point in the history
  • Loading branch information
jaredhanson committed Oct 14, 2011
1 parent 7775e52 commit bc411eb
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 8 deletions.
2 changes: 1 addition & 1 deletion lib/passport/context/http/actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ actions.success = function(user) {
var self = this;
this.passport.serializeUser(user, function(err, obj) {
if (err) { return self.next(err); }
self.req._passport.user = obj;
self.req._passport.session.user = obj;
self.req.user = user;
self.next();
});
Expand Down
16 changes: 9 additions & 7 deletions lib/passport/middleware/authentication.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,21 +47,23 @@ module.exports = function authentication() {

return function authentication(req, res, next) {
var passport = this;
req._passport = {};
req._passport.instance = passport;

if (req.session && req.session[passport._key]) {
// load existing data from session
req._passport = req.session[passport._key];
// load data from existing session
req._passport.session = req.session[passport._key];
} else if (req.session) {
// add initial data to session
// initialize new session
req.session[passport._key] = {};
req._passport = req.session[passport._key];
req._passport.session = req.session[passport._key];
} else {
// no session is available
req._passport = {};
req._passport.session = {};
}

if (req._passport.user) {
passport.deserializeUser(req._passport.user, function(err, user) {
if (req._passport.session.user) {
passport.deserializeUser(req._passport.session.user, function(err, user) {
if (err) { return next(err); }
req.user = user;
next();
Expand Down
6 changes: 6 additions & 0 deletions test/middleware/authentication-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ vows.describe('authentication').addBatch({
},
'should set internal passport on the request' : function(err, req, res) {
assert.isObject(req._passport);
assert.instanceOf(req._passport.instance, Passport);
assert.isObject(req._passport.session);
},
},

Expand Down Expand Up @@ -68,6 +70,8 @@ vows.describe('authentication').addBatch({
},
'should set internal passport on the request' : function(err, req, res) {
assert.isObject(req._passport);
assert.instanceOf(req._passport.instance, Passport);
assert.isObject(req._passport.session);
},
},

Expand Down Expand Up @@ -101,6 +105,8 @@ vows.describe('authentication').addBatch({
},
'should set internal passport on the request' : function(err, req, res) {
assert.isObject(req._passport);
assert.instanceOf(req._passport.instance, Passport);
assert.isObject(req._passport.session);
},
},
},
Expand Down

0 comments on commit bc411eb

Please sign in to comment.