node.js - PassportJS NodeJS Display Error Messages on page -
i using locomotivejs , passportjs auth on project, , found code online 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 cannot figure out how display error messages on page such "username exists" or "passwords not match" etc. can them console.log(). know how can this?
connect-flash pretty useful such situations:
// in app's config: var flash = require('connect-flash'); ... app.use(flash()); ... // in #create controller: if (err) { req.flash('error', 'your error message here'); return self.redirect(self.urlfor({ action: 'new' })); } // in #new controller: res.render('template', { errors: req.flash('error') });
and in templates, check if errors
exists (it's array) , render messages.
Comments
Post a Comment