From 25f7e2473ab5777a783dc8aabcda2920aea00b91 Mon Sep 17 00:00:00 2001 From: woosung1223 Date: Sun, 3 Sep 2023 03:23:36 +0900 Subject: [PATCH] =?UTF-8?q?refactor:=20=EB=A9=94=EC=86=8C=EB=93=9C=20?= =?UTF-8?q?=EB=B6=84=EB=A6=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../coyote/http11/handler/LoginHandler.java | 47 ++++++++++++------- 1 file changed, 29 insertions(+), 18 deletions(-) diff --git a/tomcat/src/main/java/org/apache/coyote/http11/handler/LoginHandler.java b/tomcat/src/main/java/org/apache/coyote/http11/handler/LoginHandler.java index eaeac94844..916a684a67 100644 --- a/tomcat/src/main/java/org/apache/coyote/http11/handler/LoginHandler.java +++ b/tomcat/src/main/java/org/apache/coyote/http11/handler/LoginHandler.java @@ -5,7 +5,6 @@ import java.util.UUID; import nextstep.jwp.db.InMemorySessionRepository; import nextstep.jwp.db.InMemoryUserRepository; -import nextstep.jwp.model.User; import org.apache.coyote.http11.ContentTypeParser; import org.apache.coyote.http11.message.Cookie; import org.apache.coyote.http11.message.Headers; @@ -33,12 +32,28 @@ public Response handle(Request request) throws IOException { private Response responseWhenHttpMethodIsGet(Request request) throws IOException { Headers requestHeaders = request.getHeaders(); Cookie cookie = requestHeaders.getCookie(); - String absolutePath = "login.html"; if (cookie.hasKey("JSESSIONID")) { - absolutePath = "index.html"; + return responseForLoggedIn(request); } + return responseForNotLoggedIn(request); + } + + private Response responseForLoggedIn(Request request) throws IOException { + String absolutePath = "index.html"; + String resource = findResourceWithPath(absolutePath); + Headers headers = new Headers(Map.of( + "Content-Type", ContentTypeParser.parse(absolutePath), + "Content-Length", String.valueOf(resource.getBytes().length) + )); + ResponseBody responseBody = new ResponseBody(resource); + + return Response.from(request.getHttpVersion(), HttpStatus.FOUND, + headers, responseBody); + } + private Response responseForNotLoggedIn(Request request) throws IOException { + String absolutePath = "login.html"; String resource = findResourceWithPath(absolutePath); Headers headers = new Headers(Map.of( "Content-Type", ContentTypeParser.parse(absolutePath), @@ -55,43 +70,39 @@ private Response responseWhenHttpMethodIsPost(Request request) throws IOExceptio String account = requestBody.get("account"); String password = requestBody.get("password"); - // TODO: 없는 계정으로 로그인 했을 때에도 responseWhenLoginFail로 이동 - User user = InMemoryUserRepository.findByAccountAndPassword(account, password) - .orElseThrow(); - - if (user.hasSameCredential(account, password)) { + if (InMemoryUserRepository.hasSameCredential(account, password)) { return responseWhenLoginSuccess(request); } return responseWhenLoginFail(request); } - private Response responseWhenLoginFail(Request request) throws IOException { - String absolutePath = "401.html"; + private Response responseWhenLoginSuccess(Request request) throws IOException { + UUID sessionId = saveSession(request); + String absolutePath = "index.html"; String resource = findResourceWithPath(absolutePath); Headers headers = new Headers(Map.of( "Content-Type", ContentTypeParser.parse(absolutePath), - "Content-Length", String.valueOf(resource.getBytes().length) + "Content-Length", String.valueOf(resource.getBytes().length), + "Set-Cookie", "JSESSIONID=" + sessionId )); ResponseBody responseBody = new ResponseBody(resource); - return Response.from(request.getHttpVersion(), HttpStatus.UNAUTHORIZED, + return Response.from(request.getHttpVersion(), HttpStatus.FOUND, headers, responseBody); } - private Response responseWhenLoginSuccess(Request request) throws IOException { - UUID sessionId = saveSession(request); + private Response responseWhenLoginFail(Request request) throws IOException { + String absolutePath = "401.html"; - String absolutePath = "index.html"; String resource = findResourceWithPath(absolutePath); Headers headers = new Headers(Map.of( "Content-Type", ContentTypeParser.parse(absolutePath), - "Content-Length", String.valueOf(resource.getBytes().length), - "Set-Cookie", "JSESSIONID=" + sessionId + "Content-Length", String.valueOf(resource.getBytes().length) )); ResponseBody responseBody = new ResponseBody(resource); - return Response.from(request.getHttpVersion(), HttpStatus.FOUND, + return Response.from(request.getHttpVersion(), HttpStatus.UNAUTHORIZED, headers, responseBody); }