I am using Locomotivejs and passportJS for auth on a project, and I found some code online to handle register:
AccountController.create = function() {
var account = new Account();
console.log("test");
account.email = this.param('email');
account.password = this.param('password');
account.name.first = this.param('first');
account.name.last = this.param('name.last');
var self = this;
account.save(function (err) {
if (err) {
console.log(err);
return self.redirect(self.urlFor({ action: 'new' }));
}
return self.redirect(self.urlFor({ action: 'login' }));
});
};
However I cannot figure out how to display error messages on the page such as "Username already exists" or "passwords do not match" etc. I can only get them to console.log(). Does anyone know how I can do this?
connect-flash is pretty useful for such situations:
// in your app's config:
var flash = require('connect-flash');
...
app.use(flash());
...
// in your #create controller:
if (err) {
req.flash('error', 'YOUR ERROR MESSAGE HERE');
return self.redirect(self.urlFor({ action: 'new' }));
}
// in your #new controller:
res.render('TEMPLATE', { errors: req.flash('error') });
And in your templates, check if errors exists (it's an array) and render the messages.