From b679453602a7cfb40fef92030480245744594846 Mon Sep 17 00:00:00 2001 From: soomanbaek Date: Sat, 21 Oct 2023 12:24:35 +0900 Subject: [PATCH] =?UTF-8?q?refactor:=20=EB=B3=80=EC=88=98=20=EB=84=A4?= =?UTF-8?q?=EC=9D=B4=EB=B0=8D=20=EC=88=98=EC=A0=95=20-=20`issueActiveInfoI?= =?UTF-8?q?dCookie(String)`=20->=20`issueActiveInfoIdCookieByEmail(String)?= =?UTF-8?q?`?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../toquiz/domain/member/api/AuthApi.java | 22 +++++++++---------- .../global/util/auth/CookieService.java | 4 +++- .../toquiz/member/api/AuthApiTest.java | 2 +- 3 files changed, 15 insertions(+), 13 deletions(-) diff --git a/src/main/java/blacktokkies/toquiz/domain/member/api/AuthApi.java b/src/main/java/blacktokkies/toquiz/domain/member/api/AuthApi.java index 94bfd1a..6ec5c65 100644 --- a/src/main/java/blacktokkies/toquiz/domain/member/api/AuthApi.java +++ b/src/main/java/blacktokkies/toquiz/domain/member/api/AuthApi.java @@ -30,25 +30,25 @@ ResponseEntity signUp(@RequestBody @Valid SignUpRequest signUpRe @PostMapping("/api/auth/login") ResponseEntity> login(@RequestBody @Valid LoginRequest loginRequest, - HttpServletResponse response) { + HttpServletResponse httpResponse) { AuthenticateResponse loginResponse = authService.login(loginRequest); - response.addCookie(cookieService.issueActiveInfoIdCookie(loginResponse.getEmail())); - response.addCookie(cookieService.issueRefreshTokenCookie(loginResponse.getEmail())); + httpResponse.addCookie(cookieService.issueActiveInfoIdCookieByEmail(loginResponse.getEmail())); + httpResponse.addCookie(cookieService.issueRefreshTokenCookie(loginResponse.getEmail())); return ResponseEntity.ok(new SuccessResponse<>(loginResponse)); } @PostMapping("/api/auth/logout") ResponseEntity logout( - HttpServletResponse response, + HttpServletResponse httpResponse, @AuthenticationPrincipal Member member ){ authService.logout(member); // 로그아웃 시 서버에서 activeInfoId, refreshToken을 만료하고, 클라이언트에서 accessToken을 만료 함. - response.addCookie(cookieService.expireCookie("active_info_id")); - response.addCookie(cookieService.expireCookie("refresh_token")); + httpResponse.addCookie(cookieService.expireCookie("active_info_id")); + httpResponse.addCookie(cookieService.expireCookie("refresh_token")); return ResponseEntity.ok(SuccessMessage.LOGOUT); } @@ -56,27 +56,27 @@ ResponseEntity logout( @PostMapping("api/auth/resign") public ResponseEntity deleteMyInfo( @RequestBody @Valid ResignRequest request, - HttpServletResponse response, + HttpServletResponse httpResponse, @AuthenticationPrincipal Member member, @CookieValue("active_info_id") String activeInfoId ){ authService.resign(member, request.getPassword(), activeInfoId); - response.addCookie(cookieService.expireCookie("active_info_id")); - response.addCookie(cookieService.expireCookie("refresh_token")); + httpResponse.addCookie(cookieService.expireCookie("active_info_id")); + httpResponse.addCookie(cookieService.expireCookie("refresh_token")); return ResponseEntity.ok(SuccessMessage.RESIGN); } @PostMapping ("/api/auth/refresh") ResponseEntity> refresh( - HttpServletResponse response, + HttpServletResponse httpResponse, @AuthenticationPrincipal Member member, @CookieValue(name = "refresh_token", required = false) String refreshToken ){ AuthenticateResponse refreshResponse = authService.refresh(member, refreshToken); - response.addCookie(cookieService.issueRefreshTokenCookie(refreshResponse.getEmail())); + httpResponse.addCookie(cookieService.issueRefreshTokenCookie(refreshResponse.getEmail())); return ResponseEntity.ok(new SuccessResponse<>(refreshResponse)); } diff --git a/src/main/java/blacktokkies/toquiz/global/util/auth/CookieService.java b/src/main/java/blacktokkies/toquiz/global/util/auth/CookieService.java index 26283a6..52f3cd9 100644 --- a/src/main/java/blacktokkies/toquiz/global/util/auth/CookieService.java +++ b/src/main/java/blacktokkies/toquiz/global/util/auth/CookieService.java @@ -23,6 +23,7 @@ public class CookieService { @Value("${application.security.cookie.refresh-token.expiration}") private Integer REFRESH_TOKEN_EXPIRATION; + // 익명 사용자 ActiveInfoId 쿠키 발급 public Cookie issueActiveInfoIdCookie(){ ActiveInfo activeInfo = activeInfoRepository.save(new ActiveInfo()); Cookie cookie = new Cookie("active_info_id", activeInfo.getId()); @@ -33,7 +34,8 @@ public Cookie issueActiveInfoIdCookie(){ return cookie; } - public Cookie issueActiveInfoIdCookie(String email){ + // 로그인 사용자 ActiveInfoId 쿠키 발급 + public Cookie issueActiveInfoIdCookieByEmail(String email){ Member member = memberRepository.findByEmail(email) .orElseThrow(() -> new RestApiException(MemberErrorCode.NOT_EXIST_MEMBER)); diff --git a/src/test/java/blacktokkies/toquiz/member/api/AuthApiTest.java b/src/test/java/blacktokkies/toquiz/member/api/AuthApiTest.java index 8ccd858..3c5f144 100644 --- a/src/test/java/blacktokkies/toquiz/member/api/AuthApiTest.java +++ b/src/test/java/blacktokkies/toquiz/member/api/AuthApiTest.java @@ -267,7 +267,7 @@ private ResultActions requestApi(LoginRequest request) throws Exception { doReturn(response).when(authService).login(any(LoginRequest.class)); doReturn(refreshTokenCookie).when(cookieService).issueRefreshTokenCookie(any(String.class)); - doReturn(activeInfoIdCookie).when(cookieService).issueActiveInfoIdCookie(any(String.class)); + doReturn(activeInfoIdCookie).when(cookieService).issueActiveInfoIdCookieByEmail(any(String.class)); // when final ResultActions resultActions = requestApi(request);