Advertisement
Guest User

Untitled

a guest
May 7th, 2019
141
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. exports.validateRegister = (req, res, next) => {
  2.     const sessionErros = [];
  3.     req.sanitizeBody('name');
  4.     req.checkBody('name', 'You must supply a name!').notEmpty();
  5.     req.checkBody('email', 'That Email is not valid!').isEmail();
  6.     req.sanitizeBody('email')
  7.         .normalizeEmail({
  8.             gmail_remove_dots: false,
  9.             remove_extension: false,
  10.             gmail_remove_subaddress: false
  11.         });
  12.     req.checkBody('email', 'Email is not valid')
  13.         .custom(value => {
  14.             return User.findOne({email: value}).then(user => {
  15.                 if (user) {
  16.                     console.log('find user',user);
  17.                     return Promise.reject('E-mail already in use');
  18.                 }
  19.             });
  20.         });
  21.     req.checkBody('password', 'Password Cannot be Blank!')
  22.         .notEmpty()
  23.         .isLength({min: 6})
  24.         .withMessage('Password must be at least 6 chars long');
  25.     req.checkBody('password2', 'Confirmed Password cannot be blank!').notEmpty();
  26.     req.checkBody('password2', 'Oops! Your passwords do not match').equals(req.body.password);
  27.  
  28.     const errors = req.validationErrors();
  29.  
  30.     console.log('erros', errors);
  31.  
  32.     if (errors) {
  33.         res.render('user/register', {
  34.             errors: errors,
  35.             title: 'Register',
  36.             name: req.body.name,
  37.             email: req.body.email,
  38.             password: req.body.password,
  39.             password2: req.body.password2
  40.         });
  41.         return;
  42.     }
  43.     next(); // there were no errors!
  44. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement