Advertisement
Guest User

Untitled

a guest
May 28th, 2018
125
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.14 KB | None | 0 0
  1. router.post('/register', [
  2. check('username')
  3. .isLength({ min: 4 })
  4. .withMessage('Please provide a username of at least four letters')
  5. .trim(),
  6. check('email')
  7. .isEmail()
  8. .withMessage('Please ensure that your email address is properly formatted')
  9. .trim()
  10. .normalizeEmail(),
  11. check('password')
  12. .isLength({ min: 1 })
  13. .withMessage('Please provide a password.'),
  14. check('passwordconfirmation', 'Password confirmation field must have the same value as the password field')
  15. .exists()
  16. .custom((value, { req }) => value === req.body.password)
  17. ], (req, res, next) => {
  18. const errors = validationResult(req)
  19. const mappedErrors = errors.mapped()
  20. console.log('mappedErrors obj is:', mappedErrors)
  21. const mapErrors_noErrors = Object.keys(mappedErrors).length === 0
  22. console.log('is mappedErrors an empty object (b/c there are no errors)?', mapErrors_noErrors)
  23.  
  24.  
  25.  
  26. validationResult(req);
  27.  
  28. // If there are no registration form validation errors we can move on to:
  29. // A - looking up registration request's user info by email to see if this account exists already.
  30. // B - if user obj does not exist, we can safely create one
  31. // C - if it does exist, let the visitor know so they can correct their actions
  32.  
  33. if(mapErrors_noErrors == true) {
  34. console.log('notice: there are no errors from validation result');
  35.  
  36. // A - Try to get user by email. If its possible, we know that one is taken, so cant use it
  37. return Promise.try(() => {
  38. // Here, we return the result of calling our own function. That return value is a Promise.
  39. return User_fns.getUserByEmail(req.body.email);
  40. }).then((user) => {
  41. // B - if user obj does not exist, we can safely create one
  42. // an empty user object appears as empty array: []
  43. if (user === undefined || user.length == 0) {
  44. // array empty or does not exist
  45. console.log('user obj is empty or does not exist');
  46. // safe to create user, b/c this one we searched for does not exist.
  47. // - B1. hash pw.
  48. console.log('pw avail?', req.body.password);
  49. var password = req.body.password;
  50.  
  51.  
  52. return Promise.try(() => {
  53. return User_fns.hashPassword(password);
  54. }).then((hashed_password) => {
  55. console.log('hashed_password available?', hashed_password)
  56. req.flash('success', 'Thanks for registering.')
  57.  
  58. return User_fns.createUser(req.body.username, req.body.email, hashed_password)
  59.  
  60. res.render('pages/register', {
  61. data: req.body, // { message, email }
  62. errors: errors.mapped(),
  63. env: process.env.NODE_ENV
  64. })
  65. })
  66. // - B2. insert user into users table.
  67.  
  68. // passing in the request objects from form, and hashed_password
  69.  
  70. // should even pass in some of their info, such as username, so its clear that it worked
  71. // - B3. flash confirmation
  72. } else {
  73. console.log('User object exists already, so we will notify visitor', user)
  74. // the user object based on the email already exists
  75. req.flash('success', 'Sorry, a user with that email address already exists. You may already have an account, or perhaps you mistyped your email address.')
  76. // flash some error message to the user.
  77.  
  78. res.render('pages/register', {
  79. data: req.body, // { message, email }
  80. errors: errors.mapped(),
  81. env: process.env.NODE_ENV
  82. })
  83. }
  84.  
  85.  
  86. });
  87.  
  88.  
  89. } else {
  90. // This will push data to the template to make it highlight the error fields
  91. console.log('notice: there ARE errors from validation result');
  92. res.render('pages/register', {
  93. data: req.body, // { message, email }
  94. errors: errors.mapped(),
  95. env: process.env.NODE_ENV
  96. })
  97. }
  98.  
  99. // The matchedData function returns the output of the sanitizers on our input.
  100. // i.e. this is the sanitized req.body
  101. const data = matchedData(req)
  102. console.log('Sanitized:', data)
  103.  
  104. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement