From 07597ad5f2e54659c1553c0e2dee68f7be1ac0b0 Mon Sep 17 00:00:00 2001 From: Raymond Yang Date: Tue, 16 May 2023 10:36:54 +0800 Subject: [PATCH] =?UTF-8?q?=E5=84=AA=E5=8C=96sendUnauthorized=E3=80=81send?= =?UTF-8?q?NotFound?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../kotlin/com/ray650128/extension/ResponseExtension.kt | 8 ++++---- src/main/kotlin/com/ray650128/plugins/UserRouting.kt | 8 ++++---- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/src/main/kotlin/com/ray650128/extension/ResponseExtension.kt b/src/main/kotlin/com/ray650128/extension/ResponseExtension.kt index 9d6468d..f770801 100644 --- a/src/main/kotlin/com/ray650128/extension/ResponseExtension.kt +++ b/src/main/kotlin/com/ray650128/extension/ResponseExtension.kt @@ -20,10 +20,10 @@ suspend fun ApplicationCall.sendSuccess(data: T?) { ) } -suspend fun ApplicationCall.sendUnauthorized(errorResponse: ErrorResponse) { +suspend fun ApplicationCall.sendUnauthorized() { this.respond( status = HttpStatusCode.Unauthorized, - message = errorResponse + message = ErrorResponse.UNAUTHORIZED_RESPONSE ) } @@ -34,9 +34,9 @@ suspend fun ApplicationCall.sendBadRequest(errorResponse: ErrorResponse) { ) } -suspend fun ApplicationCall.sendNotFound(errorResponse: ErrorResponse) { +suspend fun ApplicationCall.sendNotFound() { this.respond( status = HttpStatusCode.NotFound, - message = errorResponse + message = ErrorResponse.NOT_FOUND_RESPONSE ) } diff --git a/src/main/kotlin/com/ray650128/plugins/UserRouting.kt b/src/main/kotlin/com/ray650128/plugins/UserRouting.kt index 5a7fb77..ceb2299 100644 --- a/src/main/kotlin/com/ray650128/plugins/UserRouting.kt +++ b/src/main/kotlin/com/ray650128/plugins/UserRouting.kt @@ -50,18 +50,18 @@ fun Application.configureUserRouting() { } call.sendSuccess(LoginResult(request.account, token!!)) } else { - call.sendUnauthorized(ErrorResponse("Account or Password wrong.")) + call.sendBadRequest(ErrorResponse("Account or Password wrong.")) } } authenticate { post("/logout") { val account = call.authentication.principal()?.account ?: run { - call.sendUnauthorized(ErrorResponse("Unauthorized")) + call.sendUnauthorized() return@post } val user = service.findByAccount(account) ?: run { - call.sendUnauthorized(ErrorResponse("Unauthorized")) + call.sendUnauthorized() return@post } user.apply { @@ -83,7 +83,7 @@ fun Application.configureUserRouting() { val id = call.parameters["id"].toString() service.findById(id) ?.let { foundPerson -> call.respond(foundPerson.toDto()) } - ?: call.sendNotFound(ErrorResponse.NOT_FOUND_RESPONSE) + ?: call.sendNotFound() } get("/search") {