Guest User

Untitled

a guest
Mar 25th, 2018
136
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.33 KB | None | 0 0
  1. // Express Session Middleware
  2. app.use(session({
  3. secret: 'keyboard cat',
  4. resave: true,
  5. saveUninitialized: true,
  6. cookie: { secure: true }
  7. }));
  8.  
  9.  
  10. // Express Messages Middleware
  11. app.use(require('connect-flash')());
  12. app.use(function (req, res, next) {
  13. res.locals.messages = require('express-messages')(req, res);
  14. next();
  15. });
  16.  
  17. // Express Validator Middleware
  18. app.use(expressValidator({
  19. errorFormatter: function(param, msg, value) {
  20. var namespace = param.split('.')
  21. , root = namespace.shift()
  22. , formParam = root;
  23.  
  24. while(namespace.length) {
  25. formParam += '[' + namespace.shift() + ']';
  26. }
  27. return {
  28. param : formParam,
  29. msg : msg,
  30. value : value
  31. };
  32. }
  33. }));
  34.  
  35. router.post('/register', function(req, res){
  36. const name = req.body.name;
  37. const email = req.body.email;
  38. const username = req.body.username;
  39. const password = req.body.password;
  40. const password2 = req.body.password2;
  41.  
  42. req.checkBody('name', 'Name is required').notEmpty();
  43. req.checkBody('email', 'Email is required').notEmpty();
  44. req.checkBody('email', 'Email is not valid').isEmail();
  45. req.checkBody('username', 'Username is required').notEmpty();
  46. req.checkBody('password', 'Password is required').notEmpty();
  47. req.checkBody('password2', 'Passwords do not
  48. match').equals(req.body.password);
  49.  
  50. let errors = req.getValidationResult();
  51.  
  52. if(errors){
  53. res.render('register', {
  54. errors:errors
  55. });
  56. } else {
  57. let newUser = new User({
  58. name:name,
  59. email:email,
  60. username:username,
  61. password:password
  62. });
  63.  
  64. bcrypt.genSalt(10, function(err, salt){
  65. bcrypt.hash(newUser.password, salt, function(err, hash){
  66. if(err){
  67. console.log(err);
  68. }
  69. newUser.password = hash;
  70. newUser.save(function(err){
  71. if(err){
  72. console.log(err);
  73. return;
  74. } else {
  75. req.flash('success','You are now registered and can log in');
  76. res.redirect('/users/login');
  77. }
  78. });
  79. });
  80. });
  81. }
  82. });
  83.  
  84. .messages
  85. each type in Object.keys(locals.messages)
  86. each message in Object.values(locals.messages)
  87. div(class="alert alert-"+type) #{message}
  88.  
  89. != messages('message', locals)
  90. if errors
  91. each error, i in errors
  92. div(class="alert alert-danger") #{error.msg}
Add Comment
Please, Sign In to add comment