From e97ac8368f8149dc5710f7eca55e409ffa4a3d92 Mon Sep 17 00:00:00 2001 From: Fabrice Lecomte Date: Wed, 22 Jan 2020 00:09:26 +0100 Subject: [PATCH] catch WrongLoginOrPassword --- src/main/kotlin/fr/dcproject/routes/Auth.kt | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/src/main/kotlin/fr/dcproject/routes/Auth.kt b/src/main/kotlin/fr/dcproject/routes/Auth.kt index 0b55932..ea9e494 100644 --- a/src/main/kotlin/fr/dcproject/routes/Auth.kt +++ b/src/main/kotlin/fr/dcproject/routes/Auth.kt @@ -42,18 +42,21 @@ fun Route.auth( post { try { val credentials = call.receive() - val user = userRepo.findByCredentials(credentials) ?: throw BadRequestException("Username not exist or password is wrong") + val user = userRepo.findByCredentials(credentials) ?: throw WrongLoginOrPassword() call.respondText(JwtConfig.makeToken(user)) } catch (e: MismatchedInputException) { - throw BadRequestException("You must be send name and password to the request") + call.respond(HttpStatusCode.BadRequest, "You must be send name and password to the request") + } catch (e: WrongLoginOrPassword) { + call.respond(HttpStatusCode.BadRequest, e.message) } } post { val citizen = call.receive() citizen.user?.roles = listOf(User.Roles.ROLE_USER) + // TODO implement with validator + citizen.email ?: throw BadRequestException("Bad request") val created = citizenRepo.insertWithUser(citizen)?.user ?: throw BadRequestException("Bad request") - call.respondText(JwtConfig.makeToken(created)) } @@ -68,3 +71,5 @@ fun Route.auth( call.respond(HttpStatusCode.NoContent) } } + +class WrongLoginOrPassword(override val message: String = "Username not exist or password is wrong") : Exception()