From a349c2bc32e4eba793500bb7b6125f07e0014a99 Mon Sep 17 00:00:00 2001 From: Jared Hanson Date: Thu, 19 May 2022 09:30:59 -0700 Subject: [PATCH] Add option to keep session data. --- lib/http/request.js | 2 +- lib/sessionmanager.js | 4 ++++ 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/lib/http/request.js b/lib/http/request.js index 9bb27e42..705110fd 100644 --- a/lib/http/request.js +++ b/lib/http/request.js @@ -36,7 +36,7 @@ req.logIn = function(user, options, done) { if (typeof done != 'function') { throw new Error('req#login requires a callback function'); } var self = this; - this._sessionManager.logIn(this, user, function(err) { + this._sessionManager.logIn(this, user, options, function(err) { if (err) { self[property] = null; return done(err); } done(); }); diff --git a/lib/sessionmanager.js b/lib/sessionmanager.js index 30323033..193f11b4 100644 --- a/lib/sessionmanager.js +++ b/lib/sessionmanager.js @@ -21,6 +21,7 @@ SessionManager.prototype.logIn = function(req, user, options, cb) { if (!req.session) { return cb(new Error('Login sessions require session support. Did you forget to use `express-session` middleware?')); } var self = this; + var prevSession = req.session; // regenerate the session, which is good practice to help // guard against forms of session fixation @@ -33,6 +34,9 @@ SessionManager.prototype.logIn = function(req, user, options, cb) { if (err) { return cb(err); } + if (options.keepSessionData) { + Object.assign(req.session, prevSession); + } if (!req.session[self._key]) { req.session[self._key] = {}; }