From f411118191ed73c4e3bde7224d887790f8080f8d Mon Sep 17 00:00:00 2001 From: Jared Hanson Date: Wed, 15 Dec 2021 10:02:20 -0800 Subject: [PATCH] Expose sessionManager on req, and use it rather than _passport.instance. --- lib/http/request.js | 4 ++-- lib/middleware/initialize.js | 3 +++ test/http/request.test.js | 5 +++++ 3 files changed, 10 insertions(+), 2 deletions(-) diff --git a/lib/http/request.js b/lib/http/request.js index 3e6cc4e5..db74ef01 100644 --- a/lib/http/request.js +++ b/lib/http/request.js @@ -37,7 +37,7 @@ req.logIn = function(user, options, done) { if (typeof done != 'function') { throw new Error('req#login requires a callback function'); } var self = this; - this._passport.instance._sm.logIn(this, user, function(err) { + this.sessionManager.logIn(this, user, function(err) { if (err) { self[property] = null; return done(err); } done(); }); @@ -57,7 +57,7 @@ req.logOut = function() { this[property] = null; if (this._passport) { - this._passport.instance._sm.logOut(this); + this.sessionManager.logOut(this); } }; diff --git a/lib/middleware/initialize.js b/lib/middleware/initialize.js index 724982b6..eee25ad7 100644 --- a/lib/middleware/initialize.js +++ b/lib/middleware/initialize.js @@ -56,6 +56,9 @@ module.exports = function initialize(passport, options) { req.isAuthenticated = IncomingMessageExt.isAuthenticated; req.isUnauthenticated = IncomingMessageExt.isUnauthenticated; + // expose session manager + req.sessionManager = passport._sm; + if (options.userProperty) { req._userProperty = options.userProperty; } diff --git a/test/http/request.test.js b/test/http/request.test.js index 61f8f20e..62b230f8 100644 --- a/test/http/request.test.js +++ b/test/http/request.test.js @@ -201,6 +201,7 @@ describe('http.ServerRequest', function() { req.isUnauthenticated = request.isUnauthenticated; req._passport = {}; req._passport.instance = passport; + req.sessionManager = passport._sm; req.session = {}; var error; @@ -246,6 +247,7 @@ describe('http.ServerRequest', function() { req.isUnauthenticated = request.isUnauthenticated; req._passport = {}; req._passport.instance = passport; + req.sessionManager = passport._sm; req.session = {}; req._userProperty = 'currentUser'; @@ -296,6 +298,7 @@ describe('http.ServerRequest', function() { req.isUnauthenticated = request.isUnauthenticated; req._passport = {}; req._passport.instance = passport; + req.sessionManager = passport._sm; req.session = {}; req.session['passport'] = {}; @@ -378,6 +381,7 @@ describe('http.ServerRequest', function() { req.user = { id: '1', username: 'root' }; req._passport = {}; req._passport.instance = passport; + req.sessionManager = passport._sm; req.session = {}; req.session['passport'] = {}; req.session['passport'].user = '1'; @@ -409,6 +413,7 @@ describe('http.ServerRequest', function() { req._passport = {}; req._passport.instance = passport; req._userProperty = 'currentUser'; + req.sessionManager = passport._sm; req.session = {}; req.session['passport'] = {}; req.session['passport'].user = '1';