Last active
August 30, 2019 22:20
-
-
Save dularion/708836e6506d18d876b747c1168697b4 to your computer and use it in GitHub Desktop.
Grails user registration
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
def registrationData = request.JSON | |
List errors = [] | |
if (!registrationData.email) { | |
errors.add('email') | |
} | |
if (User.countByUsername(registrationData.email) > 0) { | |
errors.add('duplicate') | |
} | |
if (!registrationData.password) { | |
errors.add('passwordMissing') | |
} else if (registrationData.password.size() < 8) { | |
errors.add('passwordLength') | |
} | |
if (!registrationData.password2) { | |
errors.add('passwordMatchMissing') | |
} else if (registrationData.password != registrationData.password2) { | |
errors.add('passwordMatchFailed') | |
} | |
Boolean containsNumberAndLetter = (registrationData.password =~ /\p{Alpha}/) && | |
(registrationData.password =~ /\p{Digit}/) | |
if(!containsNumberAndLetter) { | |
errors.add('passwordContent') | |
} | |
if (errors){ | |
response.setStatus(SC_PRECONDITION_FAILED) | |
render errors as JSON | |
return | |
} | |
User user = new User() | |
user.username = registrationData.email | |
user.password = registrationData.password | |
user.enabled = true | |
user.save(failOnError: true) | |
response.setStatus(SC_CREATED) | |
render [userId: user.id] as JSON |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment