Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- router.post('/register', [
- check('username')
- .isLength({ min: 4 })
- .withMessage('Please provide a username of at least four letters')
- .trim(),
- check('email')
- .isEmail()
- .withMessage('Please ensure that your email address is properly formatted')
- .trim()
- .normalizeEmail(),
- check('password')
- .isLength({ min: 1 })
- .withMessage('Please provide a password.'),
- check('passwordconfirmation', 'Password confirmation field must have the same value as the password field')
- .exists()
- .custom((value, { req }) => value === req.body.password)
- ], (req, res, next) => {
- const errors = validationResult(req)
- const mappedErrors = errors.mapped()
- console.log('mappedErrors obj is:', mappedErrors)
- const mapErrors_noErrors = Object.keys(mappedErrors).length === 0
- console.log('is mappedErrors an empty object (b/c there are no errors)?', mapErrors_noErrors)
- validationResult(req);
- // If there are no registration form validation errors we can move on to:
- // A - looking up registration request's user info by email to see if this account exists already.
- // B - if user obj does not exist, we can safely create one
- // C - if it does exist, let the visitor know so they can correct their actions
- if(mapErrors_noErrors == true) {
- // A - Try to get user by email. If its possible, we know that one is taken, so cant use it
- return Promise.try(() => {
- // Here, we return the result of calling our own function. That return value is a Promise.
- return User_fns.getUserByEmail(req.body.email);
- }).then((user) => {
- // B - if user obj does not exist, we can safely create one
- // an empty user object appears as empty array: []
- if (user === undefined || user.length == 0) {
- // array empty or does not exist
- console.log('user obj is empty or does not exist');
- // safe to create user, b/c this one we searched for does not exist.
- // - B1. hash pw.
- console.log('pw avail?', req.body.password);
- var password = req.body.password;
- // var hashed_password = User_fns.hashPassword(password);
- // console.log('hashed pw?', hashed_password);
- return Promise.try(() => {
- return User_fns.hashPassword(password);
- }).then((hashed_password) => {
- User_fns.createUser(req.body.username, req.body.email, hashed_password)
- req.flash('success', 'Thanks for registering.')
- })
- // - B2. insert user into users table.
- // passing in the request objects from form, and hashed_password
- // should even pass in some of their info, such as username, so its clear that it worked
- // - B3. flash confirmation
- } else {
- console.log('User object exists already, so we will notify visitor', user)
- // the user object based on the email already exists
- 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.')
- // flash some error message to the user.
- }
- console.log('notice: there are no errors from validation result');
- res.render('pages/register', {
- data: req.body, // { message, email }
- errors: errors.mapped(),
- env: process.env.NODE_ENV
- })
- });
- } else {
- // This will push data to the template to make it highlight the error fields
- console.log('notice: there ARE errors from validation result');
- res.render('pages/register', {
- data: req.body, // { message, email }
- errors: errors.mapped(),
- env: process.env.NODE_ENV
- })
- }
- // The matchedData function returns the output of the sanitizers on our input.
- // i.e. this is the sanitized req.body
- const data = matchedData(req)
- console.log('Sanitized:', data)
- });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement