Skip to content

Commit

Permalink
Fix premature redirect when used with express-session
Browse files Browse the repository at this point in the history
  • Loading branch information
zypA13510 committed May 18, 2018
1 parent 2327a36 commit 1db59e7
Showing 1 changed file with 12 additions and 3 deletions.
15 changes: 12 additions & 3 deletions lib/middleware/authenticate.js
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,15 @@ module.exports = function authenticate(passport, name, options, callback) {
// accumulator for failures from each strategy in the chain
var failures = [];

function redirect(url) {
if (req.session && req.session.save && typeof req.session.save == 'function') {
return req.session.save(function() {
return res.redirect(url);
});
}
return res.redirect(url);
}

function allFailed() {
if (callback) {
if (!multi) {
Expand Down Expand Up @@ -142,7 +151,7 @@ module.exports = function authenticate(passport, name, options, callback) {
}
}
if (options.failureRedirect) {
return res.redirect(options.failureRedirect);
return redirect(options.failureRedirect);
}

// When failure handling is not delegated to the application, the default
Expand Down Expand Up @@ -255,10 +264,10 @@ module.exports = function authenticate(passport, name, options, callback) {
url = req.session.returnTo;
delete req.session.returnTo;
}
return res.redirect(url);
return redirect(url);
}
if (options.successRedirect) {
return res.redirect(options.successRedirect);
return redirect(options.successRedirect);
}
next();
}
Expand Down

0 comments on commit 1db59e7

Please sign in to comment.