You can do this using connect-flash
Below are the code fragments in different files to return the values โโentered by the user into the form when the checks are not performed when registering with the passport.
Run this command below to add a new package to package.json
npm install connect-flash --save
app.js
var flash = require('connect-flash'); app.use(flash()); // add this above passport initialize app.use(passport.initialize()); app.use(passport.session());
config / passport.js (Please focus on loading form data into flash)
passport.use('local.signup', new LocalStrategy({ usernameField: 'email', passwordField: 'password', passReqToCallback: true }, function (req, email, password, done) { req.checkBody('first_name', 'Firstname is missing').notEmpty(); req.checkBody('last_name', 'Lastname is missing').notEmpty(); req.checkBody('email', 'Invalid email').notEmpty().isEmail(); req.checkBody('password', 'Password is too short. Minimum size is 6.').notEmpty().isLength({min:6}); req.checkBody('confirm_password', 'Password and confirm password didn\'t not match').equals(req.body.password); var errors = req.validationErrors(); if (errors) { var messages = []; errors.forEach(function(error) { messages.push(error.msg); }); req.flash('formdata', req.body); // load form data into flash return done(null, false, req.flash('error', messages)); } User.findOne({'email': email}, function (err, user) { if (err) { req.flash('formdata', req.body); // load form data into flash return done(err); } if (user) { req.flash('formdata', req.body); // load form data into flash return done(null, false, {message: 'Email is already in use.'}); } var newUser = new User(); newUser.first_name = req.body.first_name; newUser.last_name = req.body.last_name; newUser.email = email; newUser.password = newUser.encryptPassword(password); newUser.save(function(err, result) { if (err) { return done(err); } return done(null, newUser); }); }); }));
routes / index.js (Please focus on the form data in flash loaded back into the variable)
router.get('/signup', function (req, res, next) { var messages = req.flash('error'); var formdata = req.flash('formdata'); // Get formdata back into a variable res.render('user/signup', {csrfToken: req.csrfToken(), messages: messages, // pass it here to access in view file hasErrors: messages.length > 0, formData: formdata[0] }); }); router.post('/signup', passport.authenticate('local.signup', { badRequestMessage: 'Please fill the form with all details', failureRedirect: '/user/signup', failureFlash: true }), function (req, res, next) { if (req.session.oldUrl) { var oldUrl = req.session.oldUrl; req.session.oldUrl = null; res.redirect(oldUrl); } else { res.redirect('/user/profile'); } });
views / signup.hbs (Please focus on the values โโin the input elements)
<form class="wow fadeInUp animated" data-wow-delay=".7s" action="/user/signup" method="post" > <input type="text" placeholder="First Name" name="first_name" value="{{ formData.first_name }}"> <input type="text" placeholder="Last Name" name="last_name" value="{{ formData.last_name }}"> <input type="text" class="email" placeholder="Email Address" name="email" value="{{ formData.email }}"> <input type="password" name="password" value="" class="lock" placeholder="Password"> <input type="password" name="confirm_password" value="" class="lock" placeholder="Confirm Password"> <input type="hidden" name="_csrf" value="{{ csrfToken }}"> <input type="submit" name="Register" value="Register"></form>
Hope this helps.