From 41d108ca0bc1fab1ca93c5473b944f5671ea8efe Mon Sep 17 00:00:00 2001 From: Choo Date: Mon, 14 Oct 2024 10:01:26 +0900 Subject: [PATCH 1/4] =?UTF-8?q?refactor:=20Member=20=EB=8F=84=EB=A9=94?= =?UTF-8?q?=EC=9D=B8=EC=97=90=20=EB=A0=88=EA=B1=B0=EC=8B=9C=20=EB=A6=AC?= =?UTF-8?q?=ED=8C=A9=ED=86=A0=EB=A7=81=201=20=EB=B0=A9=EC=95=88=20?= =?UTF-8?q?=EC=A0=81=EC=9A=A9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../auth/controller/AuthController.java | 8 ++--- .../auth/controller/CookieProducer.java | 4 +-- .../zzang/chongdae/auth/domain/AuthToken.java | 4 +++ .../chongdae/auth/domain/KakaoMemberInfo.java | 17 +++++++++ .../chongdae/auth/domain/LoginMember.java | 12 +++++++ .../chongdae/auth/domain/SignupMember.java | 19 ++++++++++ .../chongdae/auth/service/AuthClient.java | 12 +++---- .../chongdae/auth/service/AuthService.java | 35 ++++++++++--------- .../auth/service/JwtTokenProvider.java | 6 ++-- .../auth/service/dto/AuthInfoDto.java | 5 ++- .../auth/service/dto/AuthMemberDto.java | 10 ------ .../auth/service/dto/AuthTokenDto.java | 4 --- .../auth/service/dto/LoginResponse.java | 6 ++-- .../member/repository/MemberRepository.java | 4 --- .../repository/entity/MemberEntity.java | 12 ++++++- .../auth/integration/AuthIntegrationTest.java | 5 +-- .../global/helper/CookieProvider.java | 4 +-- 17 files changed, 109 insertions(+), 58 deletions(-) create mode 100644 backend/src/main/java/com/zzang/chongdae/auth/domain/AuthToken.java create mode 100644 backend/src/main/java/com/zzang/chongdae/auth/domain/KakaoMemberInfo.java create mode 100644 backend/src/main/java/com/zzang/chongdae/auth/domain/LoginMember.java create mode 100644 backend/src/main/java/com/zzang/chongdae/auth/domain/SignupMember.java delete mode 100644 backend/src/main/java/com/zzang/chongdae/auth/service/dto/AuthMemberDto.java delete mode 100644 backend/src/main/java/com/zzang/chongdae/auth/service/dto/AuthTokenDto.java diff --git a/backend/src/main/java/com/zzang/chongdae/auth/controller/AuthController.java b/backend/src/main/java/com/zzang/chongdae/auth/controller/AuthController.java index b454b7693..99948d755 100644 --- a/backend/src/main/java/com/zzang/chongdae/auth/controller/AuthController.java +++ b/backend/src/main/java/com/zzang/chongdae/auth/controller/AuthController.java @@ -1,8 +1,8 @@ package com.zzang.chongdae.auth.controller; +import com.zzang.chongdae.auth.domain.AuthToken; import com.zzang.chongdae.auth.service.AuthService; import com.zzang.chongdae.auth.service.dto.AuthInfoDto; -import com.zzang.chongdae.auth.service.dto.AuthTokenDto; import com.zzang.chongdae.auth.service.dto.KakaoLoginRequest; import com.zzang.chongdae.auth.service.dto.LoginResponse; import com.zzang.chongdae.logging.config.LoggingMasked; @@ -31,7 +31,7 @@ public ResponseEntity kakaoLogin( @RequestBody @Valid KakaoLoginRequest request, HttpServletResponse servletResponse) { AuthInfoDto authInfo = authService.kakaoLogin(request); addTokenToHttpServletResponse(authInfo.authToken(), servletResponse); - LoginResponse response = new LoginResponse(authInfo.authMember()); + LoginResponse response = new LoginResponse(authInfo.loginMember()); return ResponseEntity.ok(response); } @@ -39,12 +39,12 @@ public ResponseEntity kakaoLogin( public ResponseEntity refresh( HttpServletRequest servletRequest, HttpServletResponse servletResponse) { String refreshToken = cookieConsumer.getRefreshToken(servletRequest.getCookies()); - AuthTokenDto authToken = authService.refresh(refreshToken); + AuthToken authToken = authService.refresh(refreshToken); addTokenToHttpServletResponse(authToken, servletResponse); return ResponseEntity.ok().build(); } - private void addTokenToHttpServletResponse(AuthTokenDto authToken, HttpServletResponse servletResponse) { + private void addTokenToHttpServletResponse(AuthToken authToken, HttpServletResponse servletResponse) { List cookies = cookieExtractor.extractAuthCookies(authToken); cookieConsumer.addCookies(servletResponse, cookies); } diff --git a/backend/src/main/java/com/zzang/chongdae/auth/controller/CookieProducer.java b/backend/src/main/java/com/zzang/chongdae/auth/controller/CookieProducer.java index 71efe791a..4ef9f1499 100644 --- a/backend/src/main/java/com/zzang/chongdae/auth/controller/CookieProducer.java +++ b/backend/src/main/java/com/zzang/chongdae/auth/controller/CookieProducer.java @@ -1,6 +1,6 @@ package com.zzang.chongdae.auth.controller; -import com.zzang.chongdae.auth.service.dto.AuthTokenDto; +import com.zzang.chongdae.auth.domain.AuthToken; import jakarta.servlet.http.Cookie; import java.util.ArrayList; import java.util.List; @@ -12,7 +12,7 @@ public class CookieProducer { private static final String ACCESS_TOKEN_COOKIE_NAME = "access_token"; private static final String REFRESH_TOKEN_COOKIE_NAME = "refresh_token"; - public List extractAuthCookies(AuthTokenDto authToken) { + public List extractAuthCookies(AuthToken authToken) { List cookies = new ArrayList<>(); cookies.add(createCookie(ACCESS_TOKEN_COOKIE_NAME, authToken.accessToken())); cookies.add(createCookie(REFRESH_TOKEN_COOKIE_NAME, authToken.refreshToken())); diff --git a/backend/src/main/java/com/zzang/chongdae/auth/domain/AuthToken.java b/backend/src/main/java/com/zzang/chongdae/auth/domain/AuthToken.java new file mode 100644 index 000000000..5206664b8 --- /dev/null +++ b/backend/src/main/java/com/zzang/chongdae/auth/domain/AuthToken.java @@ -0,0 +1,4 @@ +package com.zzang.chongdae.auth.domain; + +public record AuthToken(String accessToken, String refreshToken) { +} diff --git a/backend/src/main/java/com/zzang/chongdae/auth/domain/KakaoMemberInfo.java b/backend/src/main/java/com/zzang/chongdae/auth/domain/KakaoMemberInfo.java new file mode 100644 index 000000000..5fda14aed --- /dev/null +++ b/backend/src/main/java/com/zzang/chongdae/auth/domain/KakaoMemberInfo.java @@ -0,0 +1,17 @@ +package com.zzang.chongdae.auth.domain; + +import com.zzang.chongdae.member.domain.AuthProvider; +import lombok.AllArgsConstructor; +import lombok.Getter; + +@Getter +@AllArgsConstructor +public class KakaoMemberInfo { + + private final AuthProvider provider = AuthProvider.KAKAO; + private final String loginId; + + public KakaoMemberInfo(Long kakaoId) { + this.loginId = AuthProvider.KAKAO.buildLoginId(kakaoId.toString()); + } +} diff --git a/backend/src/main/java/com/zzang/chongdae/auth/domain/LoginMember.java b/backend/src/main/java/com/zzang/chongdae/auth/domain/LoginMember.java new file mode 100644 index 000000000..ca3c05177 --- /dev/null +++ b/backend/src/main/java/com/zzang/chongdae/auth/domain/LoginMember.java @@ -0,0 +1,12 @@ +package com.zzang.chongdae.auth.domain; + +import lombok.AllArgsConstructor; +import lombok.Getter; + +@Getter +@AllArgsConstructor +public class LoginMember { + + private final Long id; + private final String nickname; +} diff --git a/backend/src/main/java/com/zzang/chongdae/auth/domain/SignupMember.java b/backend/src/main/java/com/zzang/chongdae/auth/domain/SignupMember.java new file mode 100644 index 000000000..215a08608 --- /dev/null +++ b/backend/src/main/java/com/zzang/chongdae/auth/domain/SignupMember.java @@ -0,0 +1,19 @@ +package com.zzang.chongdae.auth.domain; + +import com.zzang.chongdae.member.domain.AuthProvider; +import lombok.AllArgsConstructor; +import lombok.Getter; + +@Getter +@AllArgsConstructor +public class SignupMember { + + private final String nickname; + private final AuthProvider provider; + private final String loginId; + private final String password; + + public SignupMember(String nickname, String password, KakaoMemberInfo info) { + this(nickname, info.getProvider(), info.getLoginId(), password); + } +} diff --git a/backend/src/main/java/com/zzang/chongdae/auth/service/AuthClient.java b/backend/src/main/java/com/zzang/chongdae/auth/service/AuthClient.java index af9ad6166..e9cb208b4 100644 --- a/backend/src/main/java/com/zzang/chongdae/auth/service/AuthClient.java +++ b/backend/src/main/java/com/zzang/chongdae/auth/service/AuthClient.java @@ -1,8 +1,8 @@ package com.zzang.chongdae.auth.service; +import com.zzang.chongdae.auth.domain.KakaoMemberInfo; import com.zzang.chongdae.auth.exception.KakaoLoginExceptionHandler; import com.zzang.chongdae.auth.service.dto.KakaoLoginResponseDto; -import com.zzang.chongdae.member.domain.AuthProvider; import lombok.RequiredArgsConstructor; import org.springframework.http.HttpHeaders; import org.springframework.web.client.RestClient; @@ -11,18 +11,18 @@ public class AuthClient { private static final String BEARER_HEADER_FORMAT = "Bearer %s"; - private static final String GET_KAKAO_USER_INFO_URI = "https://kapi.kakao.com/v2/user/me"; + private static final String GET_KAKAO_MEMBER_INFO_URI = "https://kapi.kakao.com/v2/user/me"; private final RestClient restClient; - public String getKakaoUserInfo(String accessToken) { - KakaoLoginResponseDto responseDto = restClient.get() - .uri(GET_KAKAO_USER_INFO_URI) + public KakaoMemberInfo getKakaoMemberInfo(String accessToken) { + KakaoLoginResponseDto response = restClient.get() + .uri(GET_KAKAO_MEMBER_INFO_URI) .header(HttpHeaders.AUTHORIZATION, createAuthorization(accessToken)) .retrieve() .onStatus(new KakaoLoginExceptionHandler()) .body(KakaoLoginResponseDto.class); - return AuthProvider.KAKAO.buildLoginId(responseDto.id().toString()); // TODO: NPE 처리 고려하기 + return new KakaoMemberInfo(response.id()); // TODO: NPE 처리 고려하기 } private String createAuthorization(String accessToken) { diff --git a/backend/src/main/java/com/zzang/chongdae/auth/service/AuthService.java b/backend/src/main/java/com/zzang/chongdae/auth/service/AuthService.java index 9adf578fa..b345fdbbd 100644 --- a/backend/src/main/java/com/zzang/chongdae/auth/service/AuthService.java +++ b/backend/src/main/java/com/zzang/chongdae/auth/service/AuthService.java @@ -1,12 +1,13 @@ package com.zzang.chongdae.auth.service; +import com.zzang.chongdae.auth.domain.AuthToken; +import com.zzang.chongdae.auth.domain.KakaoMemberInfo; +import com.zzang.chongdae.auth.domain.LoginMember; +import com.zzang.chongdae.auth.domain.SignupMember; import com.zzang.chongdae.auth.service.dto.AuthInfoDto; -import com.zzang.chongdae.auth.service.dto.AuthMemberDto; -import com.zzang.chongdae.auth.service.dto.AuthTokenDto; import com.zzang.chongdae.auth.service.dto.KakaoLoginRequest; import com.zzang.chongdae.global.config.WriterDatabase; import com.zzang.chongdae.global.exception.MarketException; -import com.zzang.chongdae.member.domain.AuthProvider; import com.zzang.chongdae.member.exception.MemberErrorCode; import com.zzang.chongdae.member.repository.MemberRepository; import com.zzang.chongdae.member.repository.entity.MemberEntity; @@ -27,26 +28,26 @@ public class AuthService { @WriterDatabase public AuthInfoDto kakaoLogin(KakaoLoginRequest request) { - String loginId = authClient.getKakaoUserInfo(request.accessToken()); - AuthProvider provider = AuthProvider.KAKAO; - MemberEntity member = memberRepository.findByLoginId(loginId) - .orElseGet(() -> signup(provider, loginId)); - return login(member); + KakaoMemberInfo kakaoMemberInfo = authClient.getKakaoMemberInfo(request.accessToken()); + MemberEntity member = memberRepository.findByLoginId(kakaoMemberInfo.getLoginId()) + .orElseGet(() -> signup(kakaoMemberInfo)); // adaptor로 이동 + return login(member.toLoginMember()); // adaptor로 이동 } - private MemberEntity signup(AuthProvider provider, String loginId) { + private MemberEntity signup(KakaoMemberInfo kakaoMemberInfo) { + String nickname = nickNameGenerator.generate(); String password = passwordEncoder.encode(UUID.randomUUID().toString()); - MemberEntity member = new MemberEntity(nickNameGenerator.generate(), provider, loginId, password); - return memberRepository.save(member); + SignupMember signupMember = new SignupMember(nickname, password, kakaoMemberInfo); + MemberEntity member = new MemberEntity(signupMember); // adaptor로 이동 + return memberRepository.save(member); // adaptor로 이동 } - private AuthInfoDto login(MemberEntity member) { - AuthMemberDto authMember = new AuthMemberDto(member); - AuthTokenDto authToken = jwtTokenProvider.createAuthToken(member.getId().toString()); - return new AuthInfoDto(authMember, authToken); + private AuthInfoDto login(LoginMember loginMember) { + AuthToken authToken = jwtTokenProvider.createAuthToken(loginMember.getId().toString()); + return new AuthInfoDto(loginMember, authToken); } - public AuthTokenDto refresh(String refreshToken) { + public AuthToken refresh(String refreshToken) { Long memberId = jwtTokenProvider.getMemberIdByRefreshToken(refreshToken); return jwtTokenProvider.createAuthToken(memberId.toString()); } @@ -54,6 +55,6 @@ public AuthTokenDto refresh(String refreshToken) { public MemberEntity findMemberByAccessToken(String token) { Long memberId = jwtTokenProvider.getMemberIdByAccessToken(token); return memberRepository.findById(memberId) - .orElseThrow(() -> new MarketException(MemberErrorCode.NOT_FOUND)); + .orElseThrow(() -> new MarketException(MemberErrorCode.NOT_FOUND)); // TODO: 전부 도메인으로 교체 } } diff --git a/backend/src/main/java/com/zzang/chongdae/auth/service/JwtTokenProvider.java b/backend/src/main/java/com/zzang/chongdae/auth/service/JwtTokenProvider.java index 6b21e237b..3bb18a8eb 100644 --- a/backend/src/main/java/com/zzang/chongdae/auth/service/JwtTokenProvider.java +++ b/backend/src/main/java/com/zzang/chongdae/auth/service/JwtTokenProvider.java @@ -1,7 +1,7 @@ package com.zzang.chongdae.auth.service; +import com.zzang.chongdae.auth.domain.AuthToken; import com.zzang.chongdae.auth.exception.AuthErrorCode; -import com.zzang.chongdae.auth.service.dto.AuthTokenDto; import com.zzang.chongdae.global.exception.MarketException; import io.jsonwebtoken.Claims; import io.jsonwebtoken.ExpiredJwtException; @@ -36,8 +36,8 @@ public JwtTokenProvider(@Value("${security.jwt.token.access-secret-key}") String this.clock = clock; } - public AuthTokenDto createAuthToken(String payload) { - return new AuthTokenDto(createToken(payload, accessSecretKey, accessTokenExpired), + public AuthToken createAuthToken(String payload) { + return new AuthToken(createToken(payload, accessSecretKey, accessTokenExpired), createToken(payload, refreshSecretKey, refreshTokenExpired)); } diff --git a/backend/src/main/java/com/zzang/chongdae/auth/service/dto/AuthInfoDto.java b/backend/src/main/java/com/zzang/chongdae/auth/service/dto/AuthInfoDto.java index aa733d0fd..a5c6d253c 100644 --- a/backend/src/main/java/com/zzang/chongdae/auth/service/dto/AuthInfoDto.java +++ b/backend/src/main/java/com/zzang/chongdae/auth/service/dto/AuthInfoDto.java @@ -1,4 +1,7 @@ package com.zzang.chongdae.auth.service.dto; -public record AuthInfoDto(AuthMemberDto authMember, AuthTokenDto authToken) { +import com.zzang.chongdae.auth.domain.AuthToken; +import com.zzang.chongdae.auth.domain.LoginMember; + +public record AuthInfoDto(LoginMember loginMember, AuthToken authToken) { } diff --git a/backend/src/main/java/com/zzang/chongdae/auth/service/dto/AuthMemberDto.java b/backend/src/main/java/com/zzang/chongdae/auth/service/dto/AuthMemberDto.java deleted file mode 100644 index 894bfff43..000000000 --- a/backend/src/main/java/com/zzang/chongdae/auth/service/dto/AuthMemberDto.java +++ /dev/null @@ -1,10 +0,0 @@ -package com.zzang.chongdae.auth.service.dto; - -import com.zzang.chongdae.member.repository.entity.MemberEntity; - -public record AuthMemberDto(Long id, String nickname) { - - public AuthMemberDto(MemberEntity member) { - this(member.getId(), member.getNickname()); - } -} diff --git a/backend/src/main/java/com/zzang/chongdae/auth/service/dto/AuthTokenDto.java b/backend/src/main/java/com/zzang/chongdae/auth/service/dto/AuthTokenDto.java deleted file mode 100644 index a28b7bf2e..000000000 --- a/backend/src/main/java/com/zzang/chongdae/auth/service/dto/AuthTokenDto.java +++ /dev/null @@ -1,4 +0,0 @@ -package com.zzang.chongdae.auth.service.dto; - -public record AuthTokenDto(String accessToken, String refreshToken) { -} diff --git a/backend/src/main/java/com/zzang/chongdae/auth/service/dto/LoginResponse.java b/backend/src/main/java/com/zzang/chongdae/auth/service/dto/LoginResponse.java index c2fdcab28..64cdc0acf 100644 --- a/backend/src/main/java/com/zzang/chongdae/auth/service/dto/LoginResponse.java +++ b/backend/src/main/java/com/zzang/chongdae/auth/service/dto/LoginResponse.java @@ -1,8 +1,10 @@ package com.zzang.chongdae.auth.service.dto; +import com.zzang.chongdae.auth.domain.LoginMember; + public record LoginResponse(Long memberId, String nickname) { - public LoginResponse(AuthMemberDto authMember) { - this(authMember.id(), authMember.nickname()); + public LoginResponse(LoginMember loginMember) { + this(loginMember.id(), loginMember.nickname()); } } diff --git a/backend/src/main/java/com/zzang/chongdae/member/repository/MemberRepository.java b/backend/src/main/java/com/zzang/chongdae/member/repository/MemberRepository.java index d2bf2ec0d..e88ff8a96 100644 --- a/backend/src/main/java/com/zzang/chongdae/member/repository/MemberRepository.java +++ b/backend/src/main/java/com/zzang/chongdae/member/repository/MemberRepository.java @@ -6,10 +6,6 @@ public interface MemberRepository extends JpaRepository { - Optional findByPassword(String password); - - boolean existsByPassword(String password); - boolean existsByNickname(String nickname); Optional findByLoginId(String loginId); diff --git a/backend/src/main/java/com/zzang/chongdae/member/repository/entity/MemberEntity.java b/backend/src/main/java/com/zzang/chongdae/member/repository/entity/MemberEntity.java index 03fe675bf..9dd47da9f 100644 --- a/backend/src/main/java/com/zzang/chongdae/member/repository/entity/MemberEntity.java +++ b/backend/src/main/java/com/zzang/chongdae/member/repository/entity/MemberEntity.java @@ -1,5 +1,7 @@ package com.zzang.chongdae.member.repository.entity; +import com.zzang.chongdae.auth.domain.LoginMember; +import com.zzang.chongdae.auth.domain.SignupMember; import com.zzang.chongdae.global.repository.entity.BaseTimeEntity; import com.zzang.chongdae.member.domain.AuthProvider; import jakarta.persistence.Column; @@ -42,11 +44,19 @@ public class MemberEntity extends BaseTimeEntity { @NotNull private String password; // TODO: 일반 로그인 들어올 시 salt 추가 - + public MemberEntity(String nickname, AuthProvider provider, String loginId, String password) { this(null, nickname, provider, loginId, password); } + public MemberEntity(SignupMember member) { + this(member.getNickname(), member.getProvider(), member.getLoginId(), member.getPassword()); + } + + public LoginMember toLoginMember() { + return new LoginMember(id, nickname); + } + public boolean isSame(MemberEntity other) { return this.equals(other); } diff --git a/backend/src/test/java/com/zzang/chongdae/auth/integration/AuthIntegrationTest.java b/backend/src/test/java/com/zzang/chongdae/auth/integration/AuthIntegrationTest.java index 255aff60b..0636fb7a6 100644 --- a/backend/src/test/java/com/zzang/chongdae/auth/integration/AuthIntegrationTest.java +++ b/backend/src/test/java/com/zzang/chongdae/auth/integration/AuthIntegrationTest.java @@ -11,6 +11,7 @@ import com.epages.restdocs.apispec.HeaderDescriptorWithType; import com.epages.restdocs.apispec.ResourceSnippetParameters; +import com.zzang.chongdae.auth.domain.KakaoMemberInfo; import com.zzang.chongdae.auth.service.AuthClient; import com.zzang.chongdae.auth.service.dto.KakaoLoginRequest; import com.zzang.chongdae.global.integration.IntegrationTest; @@ -68,8 +69,8 @@ class KakaoLogin { @BeforeEach void setUp() { member = memberFixture.createMember("dora"); - BDDMockito.given(authClient.getKakaoUserInfo(any())) - .willReturn(member.getLoginId()); + BDDMockito.given(authClient.getKakaoMemberInfo(any())) + .willReturn(new KakaoMemberInfo(member.getLoginId())); } @DisplayName("카카오 인증 토큰으로 로그인한다.") diff --git a/backend/src/test/java/com/zzang/chongdae/global/helper/CookieProvider.java b/backend/src/test/java/com/zzang/chongdae/global/helper/CookieProvider.java index 2533d43eb..cd3f070ed 100644 --- a/backend/src/test/java/com/zzang/chongdae/global/helper/CookieProvider.java +++ b/backend/src/test/java/com/zzang/chongdae/global/helper/CookieProvider.java @@ -1,7 +1,7 @@ package com.zzang.chongdae.global.helper; +import com.zzang.chongdae.auth.domain.AuthToken; import com.zzang.chongdae.auth.service.JwtTokenProvider; -import com.zzang.chongdae.auth.service.dto.AuthTokenDto; import com.zzang.chongdae.member.repository.entity.MemberEntity; import io.restassured.http.Cookie; import io.restassured.http.Cookies; @@ -20,7 +20,7 @@ public CookieProvider(JwtTokenProvider jwtTokenProvider) { } public Cookies createCookiesWithMember(MemberEntity member) { - AuthTokenDto authToken = jwtTokenProvider.createAuthToken(member.getId().toString()); + AuthToken authToken = jwtTokenProvider.createAuthToken(member.getId().toString()); Cookie accessTokenCookie = new Cookie.Builder(ACCESS_TOKEN_COOKIE_NAME, authToken.accessToken()).build(); Cookie refreshTokenCookie = new Cookie.Builder(REFRESH_TOKEN_COOKIE_NAME, authToken.refreshToken()).build(); return new Cookies(accessTokenCookie, refreshTokenCookie); From a456c3e52e6d0eb9e919dcb8451aca92010957ea Mon Sep 17 00:00:00 2001 From: Choo Date: Mon, 14 Oct 2024 10:24:36 +0900 Subject: [PATCH 2/4] =?UTF-8?q?refactor:=20Member=20=EB=8F=84=EB=A9=94?= =?UTF-8?q?=EC=9D=B8=EC=97=90=20=EB=A0=88=EA=B1=B0=EC=8B=9C=20=EB=A6=AC?= =?UTF-8?q?=ED=8C=A9=ED=86=A0=EB=A7=81=202=20=EB=B0=A9=EC=95=88=20?= =?UTF-8?q?=EC=A0=81=EC=9A=A9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...{KakaoMemberInfo.java => KakaoMember.java} | 4 +-- .../chongdae/auth/domain/SignupMember.java | 2 +- .../chongdae/auth/service/AuthClient.java | 6 ++--- .../chongdae/auth/service/AuthService.java | 21 ++++++++------- .../auth/service/dto/LoginResponse.java | 2 +- .../repository/MemberPersistenceAdaptor.java | 26 +++++++++++++++++++ .../auth/integration/AuthIntegrationTest.java | 6 ++--- 7 files changed, 47 insertions(+), 20 deletions(-) rename backend/src/main/java/com/zzang/chongdae/auth/domain/{KakaoMemberInfo.java => KakaoMember.java} (83%) create mode 100644 backend/src/main/java/com/zzang/chongdae/member/repository/MemberPersistenceAdaptor.java diff --git a/backend/src/main/java/com/zzang/chongdae/auth/domain/KakaoMemberInfo.java b/backend/src/main/java/com/zzang/chongdae/auth/domain/KakaoMember.java similarity index 83% rename from backend/src/main/java/com/zzang/chongdae/auth/domain/KakaoMemberInfo.java rename to backend/src/main/java/com/zzang/chongdae/auth/domain/KakaoMember.java index 5fda14aed..bfac9a2f9 100644 --- a/backend/src/main/java/com/zzang/chongdae/auth/domain/KakaoMemberInfo.java +++ b/backend/src/main/java/com/zzang/chongdae/auth/domain/KakaoMember.java @@ -6,12 +6,12 @@ @Getter @AllArgsConstructor -public class KakaoMemberInfo { +public class KakaoMember { private final AuthProvider provider = AuthProvider.KAKAO; private final String loginId; - public KakaoMemberInfo(Long kakaoId) { + public KakaoMember(Long kakaoId) { this.loginId = AuthProvider.KAKAO.buildLoginId(kakaoId.toString()); } } diff --git a/backend/src/main/java/com/zzang/chongdae/auth/domain/SignupMember.java b/backend/src/main/java/com/zzang/chongdae/auth/domain/SignupMember.java index 215a08608..05ce6c800 100644 --- a/backend/src/main/java/com/zzang/chongdae/auth/domain/SignupMember.java +++ b/backend/src/main/java/com/zzang/chongdae/auth/domain/SignupMember.java @@ -13,7 +13,7 @@ public class SignupMember { private final String loginId; private final String password; - public SignupMember(String nickname, String password, KakaoMemberInfo info) { + public SignupMember(String nickname, String password, KakaoMember info) { this(nickname, info.getProvider(), info.getLoginId(), password); } } diff --git a/backend/src/main/java/com/zzang/chongdae/auth/service/AuthClient.java b/backend/src/main/java/com/zzang/chongdae/auth/service/AuthClient.java index e9cb208b4..c23cf1462 100644 --- a/backend/src/main/java/com/zzang/chongdae/auth/service/AuthClient.java +++ b/backend/src/main/java/com/zzang/chongdae/auth/service/AuthClient.java @@ -1,6 +1,6 @@ package com.zzang.chongdae.auth.service; -import com.zzang.chongdae.auth.domain.KakaoMemberInfo; +import com.zzang.chongdae.auth.domain.KakaoMember; import com.zzang.chongdae.auth.exception.KakaoLoginExceptionHandler; import com.zzang.chongdae.auth.service.dto.KakaoLoginResponseDto; import lombok.RequiredArgsConstructor; @@ -15,14 +15,14 @@ public class AuthClient { private final RestClient restClient; - public KakaoMemberInfo getKakaoMemberInfo(String accessToken) { + public KakaoMember getKakaoMember(String accessToken) { KakaoLoginResponseDto response = restClient.get() .uri(GET_KAKAO_MEMBER_INFO_URI) .header(HttpHeaders.AUTHORIZATION, createAuthorization(accessToken)) .retrieve() .onStatus(new KakaoLoginExceptionHandler()) .body(KakaoLoginResponseDto.class); - return new KakaoMemberInfo(response.id()); // TODO: NPE 처리 고려하기 + return new KakaoMember(response.id()); // TODO: NPE 처리 고려하기 } private String createAuthorization(String accessToken) { diff --git a/backend/src/main/java/com/zzang/chongdae/auth/service/AuthService.java b/backend/src/main/java/com/zzang/chongdae/auth/service/AuthService.java index b345fdbbd..a11557905 100644 --- a/backend/src/main/java/com/zzang/chongdae/auth/service/AuthService.java +++ b/backend/src/main/java/com/zzang/chongdae/auth/service/AuthService.java @@ -1,7 +1,7 @@ package com.zzang.chongdae.auth.service; import com.zzang.chongdae.auth.domain.AuthToken; -import com.zzang.chongdae.auth.domain.KakaoMemberInfo; +import com.zzang.chongdae.auth.domain.KakaoMember; import com.zzang.chongdae.auth.domain.LoginMember; import com.zzang.chongdae.auth.domain.SignupMember; import com.zzang.chongdae.auth.service.dto.AuthInfoDto; @@ -9,6 +9,7 @@ import com.zzang.chongdae.global.config.WriterDatabase; import com.zzang.chongdae.global.exception.MarketException; import com.zzang.chongdae.member.exception.MemberErrorCode; +import com.zzang.chongdae.member.repository.MemberPersistenceAdaptor; import com.zzang.chongdae.member.repository.MemberRepository; import com.zzang.chongdae.member.repository.entity.MemberEntity; import com.zzang.chongdae.member.service.NicknameGenerator; @@ -20,6 +21,7 @@ @Service public class AuthService { + private final MemberPersistenceAdaptor memberStorage; // TODO: 인터페이스화해서 persistence 영향 없게 변경 필요 private final MemberRepository memberRepository; private final PasswordEncoder passwordEncoder; private final JwtTokenProvider jwtTokenProvider; @@ -27,19 +29,18 @@ public class AuthService { private final AuthClient authClient; @WriterDatabase - public AuthInfoDto kakaoLogin(KakaoLoginRequest request) { - KakaoMemberInfo kakaoMemberInfo = authClient.getKakaoMemberInfo(request.accessToken()); - MemberEntity member = memberRepository.findByLoginId(kakaoMemberInfo.getLoginId()) - .orElseGet(() -> signup(kakaoMemberInfo)); // adaptor로 이동 - return login(member.toLoginMember()); // adaptor로 이동 + public AuthInfoDto kakaoLogin(KakaoLoginRequest request) { // TODO: LoginResponseDTO로 이름 변경하기 + KakaoMember kakaoMember = authClient.getKakaoMember(request.accessToken()); + LoginMember loginMember = memberStorage.findByLoginId(kakaoMember.getLoginId()) + .orElseGet(() -> signup(kakaoMember)); + return login(loginMember); } - private MemberEntity signup(KakaoMemberInfo kakaoMemberInfo) { + private LoginMember signup(KakaoMember kakaoMember) { String nickname = nickNameGenerator.generate(); String password = passwordEncoder.encode(UUID.randomUUID().toString()); - SignupMember signupMember = new SignupMember(nickname, password, kakaoMemberInfo); - MemberEntity member = new MemberEntity(signupMember); // adaptor로 이동 - return memberRepository.save(member); // adaptor로 이동 + SignupMember signupMember = new SignupMember(nickname, password, kakaoMember); + return memberStorage.save(signupMember); } private AuthInfoDto login(LoginMember loginMember) { diff --git a/backend/src/main/java/com/zzang/chongdae/auth/service/dto/LoginResponse.java b/backend/src/main/java/com/zzang/chongdae/auth/service/dto/LoginResponse.java index 64cdc0acf..efbd90dac 100644 --- a/backend/src/main/java/com/zzang/chongdae/auth/service/dto/LoginResponse.java +++ b/backend/src/main/java/com/zzang/chongdae/auth/service/dto/LoginResponse.java @@ -5,6 +5,6 @@ public record LoginResponse(Long memberId, String nickname) { public LoginResponse(LoginMember loginMember) { - this(loginMember.id(), loginMember.nickname()); + this(loginMember.getId(), loginMember.getNickname()); } } diff --git a/backend/src/main/java/com/zzang/chongdae/member/repository/MemberPersistenceAdaptor.java b/backend/src/main/java/com/zzang/chongdae/member/repository/MemberPersistenceAdaptor.java new file mode 100644 index 000000000..62714277b --- /dev/null +++ b/backend/src/main/java/com/zzang/chongdae/member/repository/MemberPersistenceAdaptor.java @@ -0,0 +1,26 @@ +package com.zzang.chongdae.member.repository; + +import com.zzang.chongdae.auth.domain.LoginMember; +import com.zzang.chongdae.auth.domain.SignupMember; +import com.zzang.chongdae.member.repository.entity.MemberEntity; +import java.util.Optional; +import lombok.RequiredArgsConstructor; +import org.springframework.stereotype.Component; + +@RequiredArgsConstructor +@Component +public class MemberPersistenceAdaptor { + + private final MemberRepository memberRepository; + + public Optional findByLoginId(String loginId) { + Optional member = memberRepository.findByLoginId(loginId); + return member.map(MemberEntity::toLoginMember); + } + + public LoginMember save(SignupMember signupMember) { + MemberEntity member = new MemberEntity(signupMember); + MemberEntity savedMember = memberRepository.save(member); + return savedMember.toLoginMember(); + } +} diff --git a/backend/src/test/java/com/zzang/chongdae/auth/integration/AuthIntegrationTest.java b/backend/src/test/java/com/zzang/chongdae/auth/integration/AuthIntegrationTest.java index 0636fb7a6..289aa5ec3 100644 --- a/backend/src/test/java/com/zzang/chongdae/auth/integration/AuthIntegrationTest.java +++ b/backend/src/test/java/com/zzang/chongdae/auth/integration/AuthIntegrationTest.java @@ -11,7 +11,7 @@ import com.epages.restdocs.apispec.HeaderDescriptorWithType; import com.epages.restdocs.apispec.ResourceSnippetParameters; -import com.zzang.chongdae.auth.domain.KakaoMemberInfo; +import com.zzang.chongdae.auth.domain.KakaoMember; import com.zzang.chongdae.auth.service.AuthClient; import com.zzang.chongdae.auth.service.dto.KakaoLoginRequest; import com.zzang.chongdae.global.integration.IntegrationTest; @@ -69,8 +69,8 @@ class KakaoLogin { @BeforeEach void setUp() { member = memberFixture.createMember("dora"); - BDDMockito.given(authClient.getKakaoMemberInfo(any())) - .willReturn(new KakaoMemberInfo(member.getLoginId())); + BDDMockito.given(authClient.getKakaoMember(any())) + .willReturn(new KakaoMember(member.getLoginId())); } @DisplayName("카카오 인증 토큰으로 로그인한다.") From 07f87ff95a7b5ff858b7073c960b3e431babec25 Mon Sep 17 00:00:00 2001 From: Choo Date: Mon, 14 Oct 2024 10:39:27 +0900 Subject: [PATCH 3/4] =?UTF-8?q?refactor:=20AuthService=EC=97=90=EC=84=9C?= =?UTF-8?q?=20repository=20=EC=9D=98=EC=A1=B4=EC=84=B1=20=EC=A0=9C?= =?UTF-8?q?=EA=B1=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../zzang/chongdae/auth/config/AuthWebMvcConfig.java | 6 +++--- .../chongdae/auth/config/MemberArgumentResolver.java | 12 +++++++++--- .../com/zzang/chongdae/auth/service/AuthService.java | 11 ----------- 3 files changed, 12 insertions(+), 17 deletions(-) diff --git a/backend/src/main/java/com/zzang/chongdae/auth/config/AuthWebMvcConfig.java b/backend/src/main/java/com/zzang/chongdae/auth/config/AuthWebMvcConfig.java index 78adee635..38ec2fb73 100644 --- a/backend/src/main/java/com/zzang/chongdae/auth/config/AuthWebMvcConfig.java +++ b/backend/src/main/java/com/zzang/chongdae/auth/config/AuthWebMvcConfig.java @@ -1,8 +1,8 @@ package com.zzang.chongdae.auth.config; import com.zzang.chongdae.auth.controller.CookieConsumer; -import com.zzang.chongdae.auth.service.AuthService; import com.zzang.chongdae.auth.service.JwtTokenProvider; +import com.zzang.chongdae.member.repository.MemberRepository; import java.util.List; import lombok.RequiredArgsConstructor; import org.springframework.context.annotation.Configuration; @@ -14,13 +14,13 @@ @Configuration public class AuthWebMvcConfig implements WebMvcConfigurer { - private final AuthService authService; private final CookieConsumer cookieConsumer; private final JwtTokenProvider jwtTokenProvider; + private final MemberRepository memberRepository; @Override public void addArgumentResolvers(List resolvers) { - resolvers.add(new MemberArgumentResolver(authService, cookieConsumer)); + resolvers.add(new MemberArgumentResolver(cookieConsumer, jwtTokenProvider, memberRepository)); } @Override diff --git a/backend/src/main/java/com/zzang/chongdae/auth/config/MemberArgumentResolver.java b/backend/src/main/java/com/zzang/chongdae/auth/config/MemberArgumentResolver.java index ba4ee282f..43ffbfea6 100644 --- a/backend/src/main/java/com/zzang/chongdae/auth/config/MemberArgumentResolver.java +++ b/backend/src/main/java/com/zzang/chongdae/auth/config/MemberArgumentResolver.java @@ -1,7 +1,10 @@ package com.zzang.chongdae.auth.config; import com.zzang.chongdae.auth.controller.CookieConsumer; -import com.zzang.chongdae.auth.service.AuthService; +import com.zzang.chongdae.auth.service.JwtTokenProvider; +import com.zzang.chongdae.global.exception.MarketException; +import com.zzang.chongdae.member.exception.MemberErrorCode; +import com.zzang.chongdae.member.repository.MemberRepository; import com.zzang.chongdae.member.repository.entity.MemberEntity; import jakarta.servlet.http.HttpServletRequest; import lombok.AllArgsConstructor; @@ -14,8 +17,9 @@ @AllArgsConstructor public class MemberArgumentResolver implements HandlerMethodArgumentResolver { - private final AuthService authService; private final CookieConsumer cookieConsumer; + private final JwtTokenProvider jwtTokenProvider; + private final MemberRepository memberRepository; @Override public boolean supportsParameter(MethodParameter parameter) { @@ -29,6 +33,8 @@ public MemberEntity resolveArgument(MethodParameter parameter, WebDataBinderFactory binderFactory) { HttpServletRequest request = (HttpServletRequest) webRequest.getNativeRequest(); String token = cookieConsumer.getAccessToken(request.getCookies()); - return authService.findMemberByAccessToken(token); + Long memberId = jwtTokenProvider.getMemberIdByAccessToken(token); + return memberRepository.findById(memberId) + .orElseThrow(() -> new MarketException(MemberErrorCode.NOT_FOUND)); } } diff --git a/backend/src/main/java/com/zzang/chongdae/auth/service/AuthService.java b/backend/src/main/java/com/zzang/chongdae/auth/service/AuthService.java index a11557905..4b35e37cd 100644 --- a/backend/src/main/java/com/zzang/chongdae/auth/service/AuthService.java +++ b/backend/src/main/java/com/zzang/chongdae/auth/service/AuthService.java @@ -7,11 +7,7 @@ import com.zzang.chongdae.auth.service.dto.AuthInfoDto; import com.zzang.chongdae.auth.service.dto.KakaoLoginRequest; import com.zzang.chongdae.global.config.WriterDatabase; -import com.zzang.chongdae.global.exception.MarketException; -import com.zzang.chongdae.member.exception.MemberErrorCode; import com.zzang.chongdae.member.repository.MemberPersistenceAdaptor; -import com.zzang.chongdae.member.repository.MemberRepository; -import com.zzang.chongdae.member.repository.entity.MemberEntity; import com.zzang.chongdae.member.service.NicknameGenerator; import java.util.UUID; import lombok.RequiredArgsConstructor; @@ -22,7 +18,6 @@ public class AuthService { private final MemberPersistenceAdaptor memberStorage; // TODO: 인터페이스화해서 persistence 영향 없게 변경 필요 - private final MemberRepository memberRepository; private final PasswordEncoder passwordEncoder; private final JwtTokenProvider jwtTokenProvider; private final NicknameGenerator nickNameGenerator; @@ -52,10 +47,4 @@ public AuthToken refresh(String refreshToken) { Long memberId = jwtTokenProvider.getMemberIdByRefreshToken(refreshToken); return jwtTokenProvider.createAuthToken(memberId.toString()); } - - public MemberEntity findMemberByAccessToken(String token) { - Long memberId = jwtTokenProvider.getMemberIdByAccessToken(token); - return memberRepository.findById(memberId) - .orElseThrow(() -> new MarketException(MemberErrorCode.NOT_FOUND)); // TODO: 전부 도메인으로 교체 - } } From 2520f9b56c5b9a278d9b5813961dac40482f4632 Mon Sep 17 00:00:00 2001 From: Choo Date: Mon, 14 Oct 2024 10:42:03 +0900 Subject: [PATCH 4/4] =?UTF-8?q?refactor:=20PersistenceAdaptor=20=EC=9D=B8?= =?UTF-8?q?=ED=84=B0=ED=8E=98=EC=9D=B4=EC=8A=A4=ED=99=94=ED=95=B4=EC=84=9C?= =?UTF-8?q?=20=EC=9D=98=EC=A1=B4=EC=84=B1=20=EC=97=AD=EC=A0=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/zzang/chongdae/auth/service/AuthService.java | 4 ++-- .../member/repository/MemberPersistenceAdaptor.java | 5 ++++- .../zzang/chongdae/member/service/MemberStorage.java | 12 ++++++++++++ 3 files changed, 18 insertions(+), 3 deletions(-) create mode 100644 backend/src/main/java/com/zzang/chongdae/member/service/MemberStorage.java diff --git a/backend/src/main/java/com/zzang/chongdae/auth/service/AuthService.java b/backend/src/main/java/com/zzang/chongdae/auth/service/AuthService.java index 4b35e37cd..082c353d2 100644 --- a/backend/src/main/java/com/zzang/chongdae/auth/service/AuthService.java +++ b/backend/src/main/java/com/zzang/chongdae/auth/service/AuthService.java @@ -7,7 +7,7 @@ import com.zzang.chongdae.auth.service.dto.AuthInfoDto; import com.zzang.chongdae.auth.service.dto.KakaoLoginRequest; import com.zzang.chongdae.global.config.WriterDatabase; -import com.zzang.chongdae.member.repository.MemberPersistenceAdaptor; +import com.zzang.chongdae.member.service.MemberStorage; import com.zzang.chongdae.member.service.NicknameGenerator; import java.util.UUID; import lombok.RequiredArgsConstructor; @@ -17,7 +17,7 @@ @Service public class AuthService { - private final MemberPersistenceAdaptor memberStorage; // TODO: 인터페이스화해서 persistence 영향 없게 변경 필요 + private final MemberStorage memberStorage; private final PasswordEncoder passwordEncoder; private final JwtTokenProvider jwtTokenProvider; private final NicknameGenerator nickNameGenerator; diff --git a/backend/src/main/java/com/zzang/chongdae/member/repository/MemberPersistenceAdaptor.java b/backend/src/main/java/com/zzang/chongdae/member/repository/MemberPersistenceAdaptor.java index 62714277b..26c1b1cd0 100644 --- a/backend/src/main/java/com/zzang/chongdae/member/repository/MemberPersistenceAdaptor.java +++ b/backend/src/main/java/com/zzang/chongdae/member/repository/MemberPersistenceAdaptor.java @@ -3,21 +3,24 @@ import com.zzang.chongdae.auth.domain.LoginMember; import com.zzang.chongdae.auth.domain.SignupMember; import com.zzang.chongdae.member.repository.entity.MemberEntity; +import com.zzang.chongdae.member.service.MemberStorage; import java.util.Optional; import lombok.RequiredArgsConstructor; import org.springframework.stereotype.Component; @RequiredArgsConstructor @Component -public class MemberPersistenceAdaptor { +public class MemberPersistenceAdaptor implements MemberStorage { private final MemberRepository memberRepository; + @Override public Optional findByLoginId(String loginId) { Optional member = memberRepository.findByLoginId(loginId); return member.map(MemberEntity::toLoginMember); } + @Override public LoginMember save(SignupMember signupMember) { MemberEntity member = new MemberEntity(signupMember); MemberEntity savedMember = memberRepository.save(member); diff --git a/backend/src/main/java/com/zzang/chongdae/member/service/MemberStorage.java b/backend/src/main/java/com/zzang/chongdae/member/service/MemberStorage.java new file mode 100644 index 000000000..98bdbd0a4 --- /dev/null +++ b/backend/src/main/java/com/zzang/chongdae/member/service/MemberStorage.java @@ -0,0 +1,12 @@ +package com.zzang.chongdae.member.service; + +import com.zzang.chongdae.auth.domain.LoginMember; +import com.zzang.chongdae.auth.domain.SignupMember; +import java.util.Optional; + +public interface MemberStorage { + + Optional findByLoginId(String loginId); + + LoginMember save(SignupMember signupMember); +}