Warning message with Connect-flash and Jade

I am creating a site with login authentication using node.js, express and Passport. For the registration page, I want the site to send a message when the username is already executed, right now the following code is working fine, but I would like to add a warning message with a slightly red background and highlighted text.

Code for signup.jade:

div(class="col-lg-12") form(class="form" method="post" action="/SignupForm") -if(message) div#note #{message} label(class="col-lg-12") username input(type="text" name="username") br label(class="col-lg-12") password input(type="password" name="password") br label(class="col-lg-12") email input(type="email" name="email") br input(type="submit" value="Signup") 

For my routes my code is as follows (it is inside module.exports)

 app.get('/SignupForm', function(req,res){ res.render('SignUp', {message: req.flash('signupMessage')}); }); app.post('/SignupForm', passport.authenticate('localSignup', { successRedirect: '/', failureRedirect: '/SignupForm', failureFlash: true })); 

And finally, for the Passport.js file, this is the code:

 passport.use('localSignup', new localStrategy({ usernameField: 'username', passwordField: 'password', passReqToCallback: true }, function (req, username, password, done) { process.nextTick(function () { User.findOne({'username' : username}, function (err, user) { if (err) return done(err); else if (user){ return done(null, false, req.flash('signupMessage', 'User already taken')); } else { var newUser = new User({username: username, password: password); newUser.save(function (err) { if (err) throw err; return done (null, newUser); }); } }); }); }) ); 

By the way, I already tried using a message for bootstrap, like this:

 .alert.alert-danger !{ message } 

But it immediately shows the highlighted area inside the registration page, although the message has not yet been sent.

+4
source share
2 answers

Not the best code, but it works.

Jade file:

 -if(message == 'User already taken') .alert.alert-danger #{message} 

Thus, if the message is not sent, the page does not have a selected area, and the message is displayed only when the jade files receive the line "User already done."

0
source

Well, according to you, the code above works fine, except that the message is displayed, although it has not yet been sent. To solve this problem, you can change your html / jade file as:

 if message .alert.alert-danger #{message} 

This code allows you to display a message only when it is sent by the server. If the message is not sent, the message value will not exist and the code will not be executed.

Hope this helps .. unless you provide more details by editing the question.

+2
source

All Articles