Check if username/email is taken in registration

This commit is contained in:
William Brawner 2023-06-17 10:13:32 -06:00
parent 1eab86489b
commit 694743e3f2

View file

@ -49,12 +49,24 @@ fun Application.userRoutes(
errorResponse(HttpStatusCode.BadRequest, "Password must not be null or blank")
return@post
}
val existingUser = userRepository.findAll(nameOrEmail = request.username).firstOrNull()
?: request.email?.let {
return@let if (it.isBlank()) {
null
} else {
userRepository.findAll(nameOrEmail = it).firstOrNull()
}
}
existingUser?.let {
errorResponse(HttpStatusCode.BadRequest, "Username or email already taken")
return@post
}
call.respond(
userRepository.save(
User(
name = request.username,
password = request.password.hash(),
email = request.email
email = if (request.email.isNullOrBlank()) null else request.email
)
).asResponse()
)