Created
January 25, 2014 02:00
-
-
Save aroman/8610661 to your computer and use it in GitHub Desktop.
This function's cyclomatic complexity is too high. (9)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
app.post('/join', function (req, res) { | |
var errors = {}; | |
if (!validator.isEmail(req.body.email)) { | |
errors.email = "Invalid email address"; | |
} | |
if (!validator.equals(req.body.password, req.body.verify)) { | |
errors.verify = "Passwords don't match"; | |
} | |
if (!validator.isLength(req.body.password, 6, 30)) { | |
errors.password = "Must be 6-30 characters"; | |
} | |
if (!validator.isLength(req.body.firstname, 2, 20)) { | |
errors.firstname = "2-20 characters"; | |
} | |
else if (!validator.isAlpha(req.body.firstname)) { | |
errors.firstname = "Letters only"; | |
} | |
else if (!validator.isLength(req.body.lastname, 1, 20)) { | |
errors.lastname = "1-20 characters"; | |
} | |
else if (!validator.isAlpha(req.body.lastname)) { | |
errors.lastname = "Letters only"; | |
} | |
if (_.isEmpty(errors)) { | |
var user = User(_.pick(req.body, ['email', 'password', 'firstname', 'lastname'])); | |
user.save(function (err) { | |
if (err) { | |
if (err.code === 11000) { // 11000 is duplicate key error | |
errors.email = "Already in use"; | |
} else { // We have an error, but it's not a duplicate key error | |
errors.misc = err; | |
} | |
return tryAgain(); | |
} else { | |
mail.sendVerifyEmail(user); | |
passport.authenticate('local', { successRedirect: '/' })(req, res); | |
} | |
}); | |
} else { | |
return tryAgain(); | |
} | |
function tryAgain () { | |
req.flash('values', _.omit(req.body, ['password', 'verify'])); | |
req.flash('errors', errors); | |
return res.redirect('/join'); | |
} | |
}); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment