From 50963652693ed44683b92879e40e31be14396595 Mon Sep 17 00:00:00 2001 From: hoyeonyy Date: Fri, 11 Oct 2024 14:21:13 +0900 Subject: [PATCH] =?UTF-8?q?refactor:=20=EA=B5=AC=EA=B8=80=20=EC=9D=B4?= =?UTF-8?q?=EB=A6=84=20=EB=B0=98=ED=99=98=20=EB=B3=80=EA=B2=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../backend/auth/business/GoogleAuthService.java | 14 +++++++++++--- .../mouda/backend/auth/implement/LoginManager.java | 11 ++++++++--- .../backend/member/implement/MemberWriter.java | 4 ++++ .../member/infrastructure/MemberRepository.java | 12 +++++++++--- 4 files changed, 32 insertions(+), 9 deletions(-) diff --git a/backend/src/main/java/mouda/backend/auth/business/GoogleAuthService.java b/backend/src/main/java/mouda/backend/auth/business/GoogleAuthService.java index 46706841a..bcd8b82f1 100644 --- a/backend/src/main/java/mouda/backend/auth/business/GoogleAuthService.java +++ b/backend/src/main/java/mouda/backend/auth/business/GoogleAuthService.java @@ -21,10 +21,18 @@ public LoginResponse oauthLogin(GoogleOauthRequest googleOauthRequest) { String name = googleOauthManager.getMemberName(googleOauthRequest.idToken()); String socialLoginId = googleOauthManager.getSocialLoginId(googleOauthRequest.idToken()); if (googleOauthRequest.memberId() != null) { - String accessToken = loginManager.updateOauth(googleOauthRequest.memberId(), OauthType.GOOGLE, - socialLoginId); - return new LoginResponse(accessToken); + return transferKakao(googleOauthRequest, socialLoginId); } + return processGoogleLogin(socialLoginId, name); + } + + private LoginResponse transferKakao(GoogleOauthRequest googleOauthRequest, String socialLoginId) { + String accessToken = loginManager.updateOauth(googleOauthRequest.memberId(), OauthType.GOOGLE, + socialLoginId); + return new LoginResponse(accessToken); + } + + private LoginResponse processGoogleLogin(String socialLoginId, String name) { LoginProcessResult loginProcessResult = loginManager.processSocialLogin(OauthType.GOOGLE, socialLoginId, name); return new LoginResponse(loginProcessResult.accessToken()); } diff --git a/backend/src/main/java/mouda/backend/auth/implement/LoginManager.java b/backend/src/main/java/mouda/backend/auth/implement/LoginManager.java index 2bd651811..b610c6c4e 100644 --- a/backend/src/main/java/mouda/backend/auth/implement/LoginManager.java +++ b/backend/src/main/java/mouda/backend/auth/implement/LoginManager.java @@ -29,10 +29,15 @@ public class LoginManager { public LoginProcessResult processSocialLogin(OauthType oauthType, String socialLoginId, String name) { Optional member = memberRepository.findByLoginDetail_SocialLoginId(socialLoginId); - if (member.isPresent()) { - return new LoginProcessResult(member.get().getId(), accessTokenProvider.provide(member.get())); - } + return member.map(value -> { + memberWriter.updateName(value.getId(), name); + return new LoginProcessResult(value.getId(), accessTokenProvider.provide(value)); + }) + .orElseGet(() -> processKakaoLogin(oauthType, socialLoginId, name)); + + } + private LoginProcessResult processKakaoLogin(OauthType oauthType, String socialLoginId, String name) { if (oauthType == OauthType.KAKAO) { throw new AuthException(HttpStatus.BAD_REQUEST, AuthErrorMessage.KAKAO_CANNOT_SIGNUP); } diff --git a/backend/src/main/java/mouda/backend/member/implement/MemberWriter.java b/backend/src/main/java/mouda/backend/member/implement/MemberWriter.java index b8812bd0d..7854152c3 100644 --- a/backend/src/main/java/mouda/backend/member/implement/MemberWriter.java +++ b/backend/src/main/java/mouda/backend/member/implement/MemberWriter.java @@ -21,6 +21,10 @@ public void updateLoginDetail(long memberId, OauthType oauthType, String socialL memberRepository.updateLoginDetail(memberId, oauthType, socialLoginId); } + public void updateName(long memberId, String name) { + memberRepository.updateName(memberId, name); + } + public void remove(Member member) { memberRepository.delete(member); } diff --git a/backend/src/main/java/mouda/backend/member/infrastructure/MemberRepository.java b/backend/src/main/java/mouda/backend/member/infrastructure/MemberRepository.java index 6e4575cd0..67ba81b7b 100644 --- a/backend/src/main/java/mouda/backend/member/infrastructure/MemberRepository.java +++ b/backend/src/main/java/mouda/backend/member/infrastructure/MemberRepository.java @@ -6,7 +6,6 @@ import org.springframework.data.jpa.repository.Modifying; import org.springframework.data.jpa.repository.Query; import org.springframework.data.repository.query.Param; -import org.springframework.transaction.annotation.Transactional; import mouda.backend.member.domain.Member; import mouda.backend.member.domain.OauthType; @@ -15,13 +14,20 @@ public interface MemberRepository extends JpaRepository { Optional findByLoginDetail_SocialLoginId(String socialLoginId); - @Transactional @Query(""" - UPDATE Member m + UPDATE Member m SET m.loginDetail.oauthType = :oauthType, m.loginDetail.socialLoginId = :socialLoginId WHERE m.id = :memberId """) @Modifying void updateLoginDetail(@Param("memberId") long memberId, @Param("oauthType") OauthType oauthType, @Param("socialLoginId") String socialLoginId); + + @Query(""" + UPDATE Member m + SET m.name = :name + WHERE m.id = :memberId + """) + @Modifying + void updateName(@Param("memberId") long memberId, @Param("name") String name); }