Chathura Buddika Chathura Buddika - 1 year ago 42
Node.js Question

node js passport js username field error and password field error doesn't display when the form submiting

I'm trying to create a sign up page all the functionalities are working fine. But, if username field and password field submitted empty, the page redirect to the same page but it doesn't show where the error coming from. Can you please anyone help me to fix this. Why is this hapening? I'll put the code below i created.

thank you

passport.use('local-signup', new LocalStrategy({
usernameField : 'username',
passwordField : 'password',
passReqToCallback : true
},
function(req, username, password, done) {
process.nextTick(function() {
if(req.body.first == "" || req.body.last == "" || req.body.email == ""){
return done(null, false, req.flash('signupMessage', 'Fields must be required'));
}
if(username == ""){
return done(null, false, req.flash('signupMessage', 'Username must be required'));
}
User.findOne({'email': req.body.email}, function(err, user1) {
if (err) return done(err);
User.findOne({'local.usernreqame': username}, function(err, user2){
if (err) return done(err);
if (user1) {
return done(null, false, req.flash('signupMessage', 'That email is already exist.'));
}
if (user2) {
return done(null, false, req.flash('signupMessage', 'That username is already exist.'));
}
else {
var newUser = new User();
newUser.local.username = username;
newUser.local.password = newUser.generateHash(password);
newUser.usertype = "592000f0161d63ac334358d3";
newUser.first = req.body.first;
newUser.last = req.body.last;
newUser.email = req.body.email;
newUser.save(function(err) {
if (err)
throw err;
return done(null, newUser);
});
}
})
});
});
}));


all the routes i created for this app

module.exports = function(app, passport){
app.get('/home', function(req, res){
res.render('index');
});

app.get('/sign-up', function(req, res){
res.render('signup', { message: req.flash('signupMessage') });
});

app.post('/sign-up', passport.authenticate('local-signup', {
successRedirect : '/profile',
failureRedirect : '/sign-up',
failureFlash : true
}));

app.get('/profile', isLoggedIn, function(req, res) {
res.render('users/profile', {
user : req.user
});
});

app.get('*', function(req, res){
res.render('404');
});
}
function isLoggedIn(req, res, next) {
if (req.isAuthenticated()) return next();
res.redirect('/');
}


Here you can see the template file also

<form method="post" action="/sign-up">
<input type="text" name="first" placeholder="Enter Firstname"><br/>
<input type="text" name="last" placeholder="Enter Lastname" ><br/>
<input type="email" name="email" placeholder="Enter E-mail" ><br/>
<input type="text" name="username" placeholder="Enter username"><br/>
<input type="password" name="password" placeholder="Enter Password"><br/>
<input type="submit" value="Sign Up">
</form>

<% if (message.length > 0) { %>
<div><%=message%></div>
<% } %>

Answer Source

I'm not sure what is going on. But lets try something.

I believe, your flash message its been erased.

Look:

Here is your code:

app.post('/sign-up', passport.authenticate('local-signup', {
        successRedirect : '/profile',
        failureRedirect : '/sign-up',
        failureFlash : true // you are passing this parameter
    }));

Look at the passport code:

...
if (options.failureFlash) {
        var flash = options.failureFlash; // flash here is 'true'
        if (typeof flash == 'string') { // not 'string'
          flash = { type: 'error', message: flash };
        }
        flash.type = flash.type || 'error'; // 'flash.type' is undefined, 'flash' is 'true', so not is gonna happen

        var type = flash.type || challenge.type || 'error'; // 'type' now is 'error'
        msg = flash.message || challenge.message || challenge;
        if (typeof msg == 'string') {
          req.flash(type, msg); // value assigned: ['error', 'yourmessage']
        }
      }
...

So, I believe, if your call:

app.get('/sign-up', function(req, res){
        res.render('signup', { message: req.flash('error') }); // changing 'signupMessage' to error, it would works.
    });

Or, you can change the failureFlash parameter to and object:

app.post('/sign-up', passport.authenticate('local-signup', {
            successRedirect : '/profile',
            failureRedirect : '/sign-up',
            failureFlash : { type: 'signupMessage' }
        }));

and keep this intact:

res.render('signup', { message: req.flash('signupMessage') });

Let me know if it works.

Cheers!

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download