· 8 years ago · May 28, 2018, 09:04 PM
1router.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
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 // var hashed_password = User_fns.hashPassword(password);
51 // console.log('hashed pw?', hashed_password);
52
53 return Promise.try(() => {
54 return User_fns.hashPassword(password);
55 }).then((hashed_password) => {
56 User_fns.createUser(req.body.username, req.body.email, hashed_password)
57 req.flash('success', 'Thanks for registering.')
58
59 })
60 // - B2. insert user into users table.
61
62 // passing in the request objects from form, and hashed_password
63
64 // should even pass in some of their info, such as username, so its clear that it worked
65 // - B3. flash confirmation
66 } else {
67 console.log('User object exists already, so we will notify visitor', user)
68 // the user object based on the email already exists
69 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.')
70 // flash some error message to the user.
71
72 }
73
74 console.log('notice: there are no errors from validation result');
75 res.render('pages/register', {
76 data: req.body, // { message, email }
77 errors: errors.mapped(),
78 env: process.env.NODE_ENV
79 })
80 });
81
82
83 } else {
84 // This will push data to the template to make it highlight the error fields
85 console.log('notice: there ARE errors from validation result');
86 res.render('pages/register', {
87 data: req.body, // { message, email }
88 errors: errors.mapped(),
89 env: process.env.NODE_ENV
90 })
91 }
92
93 // The matchedData function returns the output of the sanitizers on our input.
94 // i.e. this is the sanitized req.body
95 const data = matchedData(req)
96 console.log('Sanitized:', data)
97
98});