Skip to content

Instantly share code, notes, and snippets.

@dularion
Last active August 30, 2019 22:20
Show Gist options
  • Save dularion/708836e6506d18d876b747c1168697b4 to your computer and use it in GitHub Desktop.
Save dularion/708836e6506d18d876b747c1168697b4 to your computer and use it in GitHub Desktop.
Grails user registration
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