passport
passport copied to clipboard
passport.authenticate() successFlash option doesn't work after upgrade to Passport 0.6.0.
Expected behavior
I expect a flash message to showing up when a user is logged successfully with successFlash option of passport.authenticate(), like this:
//passport.js
passport.use('local.signin', new LocalStrategy({
usernameField: 'email',
passwordField: 'pass',
passReqToCallback: true
}, async (req, username, password, done) => {
const query = await pool.query('SELECT * from users WHERE email = ?', [username]);
if (query.length > 0) {
const user = query[0];
const validPass = await helpers.matchPass(password, user.password);
if (validPass === true) {
done(null, user, req.flash('success', "Welcome!")); // This doesn't works
} else {
done(null, false, req.flash('error', "Incorrect password")); // This works
};
} else {
return done(null, false, req.flash('error', "Email doesn't exist")); // This works
};
}));
//authentication.js
router.post('/signin', (req, res, next) => {
passport.authenticate('local.signin', {
successRedirect: '/home',
successFlash: true, // This doesn't works
failureRedirect: '/signin',
failureFlash: true // This works
})(req, res, next);
});
Actual behavior
Using the above code I only get failureFlash messages displayed successfully, but if the user log in correctly, req.flash('success', "Welcome!")
just doesn't show up and doesn't show any errors in the console either.
Extra information
This is happening after update to [email protected], but if I downgrade to [email protected] or [email protected], everything works as expected.
Steps to reproduce
Just using req.flash('success', 'message')
in a LocalStrategy
and then using successFlash
option in passport.authenticate()
.
passport.use('local.signin', new LocalStrategy({
usernameField: 'user',
passwordField: 'pass',
passReqToCallback: true
}, async (req, username, password, done) => {
// Some code
done(null, user, req.flash('success', 'message')); // This doesn't works
// Or
done(null, false, req.flash('error', 'message')); // This works
}));
router.post('/signin', (req, res, next) => {
passport.authenticate('local.signin', {
successRedirect: '/home',
successFlash: true, // This doesn't works
failureRedirect: '/signin',
failureFlash: true // This works
})(req, res, next);
});
Environment
- Operating System: Windows 10 21H2 (OS Build 19044.1766)
- Node version: v18.4.0
- passport version: [email protected]