Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix premature redirect when used with express-session #680

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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