Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Send data back with the Passport js failureRedirect method

I have a Passport js local signup strategy that makes use of the successRedirect and failureRedirect methods. The problem is, if there is an error with the signup process, Passport will simply redirect back to the signup form, with no data in the form.

app.post('/signup', passport.authenticate('local-signup', {
        successRedirect: '/signup/avatar', // redirect to the secure profile section
        failureRedirect: '/signup', // redirect back to the signup page if there is an error
        failureFlash: true // allow flash messages
    }));

I have the following condition inside my local signup strategy that will send a failure message to the user if the two provided passwords do not match.

if(req.body.password != req.body.confirm_password){
                                console.log('Passwords do not match');
                                return done(null, false, req.flash('signupMessage', 'Your passwords do not match'));
                            }

Along with the flash message, I want to send data back to the /signup route and re-populate the form with the data the user has provided.

Is there a way to send the form data back to the browser on failure?

like image 519
Tom Avatar asked Oct 27 '25 08:10

Tom


1 Answers

The answer was much simpler than I expected.

The validation should happen before the data is sent to Passport js, then if the data is not valid, the /signup template can be re-rendered and the data passed back.

The solution was in this question: NodeJS express-validator with passport

like image 161
Tom Avatar answered Oct 30 '25 00:10

Tom



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!