Skip to content

Commit

Permalink
Obtain _userProperty from req, rather than Passport instance.
Browse files Browse the repository at this point in the history
  • Loading branch information
jaredhanson committed Sep 23, 2021
1 parent e20e3f9 commit 761a8d2
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 21 deletions.
16 changes: 3 additions & 13 deletions lib/http/request.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,7 @@ req.logIn = function(user, options, done) {
}
options = options || {};

var property = 'user';
if (this._passport && this._passport.instance) {
property = this._passport.instance._userProperty || 'user';
}
var property = this._userProperty || 'user';
var session = (options.session === undefined) ? true : options.session;

this[property] = user;
Expand All @@ -56,10 +53,7 @@ req.logIn = function(user, options, done) {
*/
req.logout =
req.logOut = function() {
var property = 'user';
if (this._passport && this._passport.instance) {
property = this._passport.instance._userProperty || 'user';
}
var property = this._userProperty || 'user';

this[property] = null;
if (this._passport) {
Expand All @@ -74,11 +68,7 @@ req.logOut = function() {
* @api public
*/
req.isAuthenticated = function() {
var property = 'user';
if (this._passport && this._passport.instance) {
property = this._passport.instance._userProperty || 'user';
}

var property = this._userProperty || 'user';
return (this[property]) ? true : false;
};

Expand Down
7 changes: 6 additions & 1 deletion lib/middleware/initialize.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,8 @@ var IncomingMessageExt = require('../http/request');
* @return {Function}
* @api public
*/
module.exports = function initialize(passport) {
module.exports = function initialize(passport, options) {
options = options || {};

return function initialize(req, res, next) {
req.login =
Expand All @@ -55,6 +56,10 @@ module.exports = function initialize(passport) {
req.isAuthenticated = IncomingMessageExt.isAuthenticated;
req.isUnauthenticated = IncomingMessageExt.isUnauthenticated;

if (options.userProperty) {
req._userProperty = options.userProperty;
}

req._passport = {};
req._passport.instance = passport;

Expand Down
3 changes: 1 addition & 2 deletions lib/strategies/session.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,7 @@ SessionStrategy.prototype.authenticate = function(req, options) {
if (!user) {
delete req.session[self._key].user;
} else {
// TODO: Remove instance access
var property = req._passport.instance._userProperty || 'user';
var property = req._userProperty || 'user';
req[property] = user;
}
self.pass();
Expand Down
8 changes: 4 additions & 4 deletions test/http/request.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,6 @@ describe('http.ServerRequest', function() {

describe('not establishing a session and setting custom user property', function() {
var passport = new Passport();
passport._userProperty = 'currentUser';

var req = new Object();
req.login = request.login;
Expand All @@ -89,6 +88,7 @@ describe('http.ServerRequest', function() {
req._passport.instance = passport;
req.session = {};
req.session['passport'] = {};
req._userProperty = 'currentUser';

var error;

Expand Down Expand Up @@ -239,7 +239,6 @@ describe('http.ServerRequest', function() {
passport.serializeUser(function(user, done) {
done(null, user.id);
});
passport._userProperty = 'currentUser';

var req = new Object();
req.login = request.login;
Expand All @@ -248,6 +247,7 @@ describe('http.ServerRequest', function() {
req._passport = {};
req._passport.instance = passport;
req.session = {};
req._userProperty = 'currentUser';

var error;

Expand Down Expand Up @@ -408,7 +408,7 @@ describe('http.ServerRequest', function() {
req.currentUser = { id: '1', username: 'root' };
req._passport = {};
req._passport.instance = passport;
req._passport.instance._userProperty = 'currentUser';
req._userProperty = 'currentUser';
req.session = {};
req.session['passport'] = {};
req.session['passport'].user = '1';
Expand Down Expand Up @@ -472,7 +472,7 @@ describe('http.ServerRequest', function() {
req.currentUser = { id: '1', username: 'root' };
req._passport = {};
req._passport.instance = {};
req._passport.instance._userProperty = 'currentUser';
req._userProperty = 'currentUser';

it('should be authenticated', function() {
expect(req.isAuthenticated()).to.be.true;
Expand Down
2 changes: 1 addition & 1 deletion test/strategies/session.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ describe('SessionStrategy', function() {

req._passport = {};
req._passport.instance = {};
req._passport.instance._userProperty = 'currentUser';
req._userProperty = 'currentUser';
req.session = {};
req.session['passport'] = {};
req.session['passport'].user = '123456';
Expand Down

0 comments on commit 761a8d2

Please sign in to comment.