Guest User

Untitled

a guest
Dec 9th, 2018
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.18 KB | None | 0 0
  1. router.post('/register', async (req, res) => {
  2. const newUser = await usersDb();
  3. // Define the user
  4. const email = req.body.email;
  5. const username = req.body.username;
  6. const password = req.body.password;
  7. const confirmPassword = req.body.confirmPassword;
  8.  
  9. req.check('email', 'Email is required.').notEmpty();
  10. req.check('email', 'Email provided is not valid.').isEmail();
  11. req.check('username', 'Username is required.').notEmpty();
  12. req.check('password', 'Password is required.').notEmpty();
  13. req.check('confirmPassword', 'Confirm password is required.').notEmpty();
  14.  
  15. // Start user already exist check
  16. let emailCheck = await newUser.findOne({ 'email': email });
  17. let userNameCheck = await newUser.findOne({ 'username': username});
  18.  
  19. req.check('email', 'This email is taken').custom(async value => value === emailCheck.email);
  20.  
  21. // Get errors
  22. let errors = req.validationErrors();
  23. if (errors) {
  24. console.log(errors);
  25. res.render('index', {
  26. errors: errors
  27. });
  28. } else { // If no pre-errors, continue with registration check
  29.  
  30. if (!errors) {
  31. console.log('Run this block if no errors');
  32. }
  33.  
  34. }
  35. });
Add Comment
Please, Sign In to add comment