diff --git a/backend/src/main/java/mouda/backend/auth/business/AppleAuthService.java b/backend/src/main/java/mouda/backend/auth/business/AppleAuthService.java index 1bb60aae..0adf4813 100644 --- a/backend/src/main/java/mouda/backend/auth/business/AppleAuthService.java +++ b/backend/src/main/java/mouda/backend/auth/business/AppleAuthService.java @@ -1,5 +1,7 @@ package mouda.backend.auth.business; +import java.util.Optional; + import org.springframework.http.HttpStatus; import org.springframework.stereotype.Service; @@ -32,6 +34,10 @@ public LoginResponse login(String idToken, String user) { } private LoginResponse handleNewUser(String user, String identifier) { + Optional member = memberFinder.getByIdentifier(identifier); + if (member.isPresent()) { + return new LoginResponse(accessTokenProvider.provide(member.get()), member.get().isConverted()); + } Member joinedMember = join(identifier, user); return new LoginResponse(accessTokenProvider.provide(joinedMember), joinedMember.isConverted()); } diff --git a/backend/src/main/java/mouda/backend/auth/implement/AppleUserInfoProvider.java b/backend/src/main/java/mouda/backend/auth/implement/AppleUserInfoProvider.java index 7003f367..bfce1e52 100644 --- a/backend/src/main/java/mouda/backend/auth/implement/AppleUserInfoProvider.java +++ b/backend/src/main/java/mouda/backend/auth/implement/AppleUserInfoProvider.java @@ -32,7 +32,7 @@ public String getName(String user) { JsonNode node = objectMapper.readTree(user); String firstName = node.path("name").path("firstName").asText(); String lastName = node.path("name").path("lastName").asText(); - return firstName + lastName; + return lastName + firstName; } catch (JsonProcessingException exception) { throw new AuthException(HttpStatus.BAD_REQUEST, AuthErrorMessage.APPLE_USER_BAD_REQUEST); } diff --git a/backend/src/main/java/mouda/backend/member/implement/MemberFinder.java b/backend/src/main/java/mouda/backend/member/implement/MemberFinder.java index c080f765..3547ba27 100644 --- a/backend/src/main/java/mouda/backend/member/implement/MemberFinder.java +++ b/backend/src/main/java/mouda/backend/member/implement/MemberFinder.java @@ -1,5 +1,7 @@ package mouda.backend.member.implement; +import java.util.Optional; + import org.springframework.http.HttpStatus; import org.springframework.stereotype.Component; @@ -15,6 +17,10 @@ public class MemberFinder { private final MemberRepository memberRepository; + public Optional getByIdentifier(String identifier) { + return memberRepository.findByLoginDetail_Identifier(identifier); + } + public Member findActiveOrDeletedByIdentifier(String identifier) { return memberRepository.findActiveOrDeletedByIdentifier(identifier) .orElseThrow(() -> new AuthException(HttpStatus.NOT_FOUND, AuthErrorMessage.MEMBER_NOT_FOUND)); 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 386ba393..a917bfc3 100644 --- a/backend/src/main/java/mouda/backend/member/infrastructure/MemberRepository.java +++ b/backend/src/main/java/mouda/backend/member/infrastructure/MemberRepository.java @@ -13,6 +13,8 @@ public interface MemberRepository extends JpaRepository { + Optional findByLoginDetail_Identifier(String identifier); + @Query(""" SELECT m FROM Member m WHERE m.loginDetail.identifier = :identifier AND (m.memberStatus = 'ACTIVE' OR m.memberStatus = 'DELETED') diff --git a/backend/src/main/resources/application-dev.yml b/backend/src/main/resources/application-dev.yml index e7450946..eb15ca11 100644 --- a/backend/src/main/resources/application-dev.yml +++ b/backend/src/main/resources/application-dev.yml @@ -19,7 +19,7 @@ oauth: redirect-uri: https://dev.mouda.site/oauth/google apple: redirect-uri: https://api.dev.mouda.site/v1/auth/apple - redirection: https://dev.mouda.site/oauth/apple?token= + redirection: https://dev.mouda.site/oauth/apple?token=%s&isConverted=%s aws: region: diff --git a/backend/src/main/resources/application-prod.yml b/backend/src/main/resources/application-prod.yml index ccd1ef75..8ac4cbe4 100644 --- a/backend/src/main/resources/application-prod.yml +++ b/backend/src/main/resources/application-prod.yml @@ -35,6 +35,7 @@ oauth: redirect-uri: https://mouda.site/oauth/google apple: redirect-uri: https://mouda.site/v1/auth/apple + redirection: https://test.mouda.site/oauth/apple?token=%s&isConverted=%s aws: region: diff --git a/backend/src/test/java/mouda/backend/auth/business/AppleAuthServiceTest.java b/backend/src/test/java/mouda/backend/auth/business/AppleAuthServiceTest.java index dd224a42..398f3af3 100644 --- a/backend/src/test/java/mouda/backend/auth/business/AppleAuthServiceTest.java +++ b/backend/src/test/java/mouda/backend/auth/business/AppleAuthServiceTest.java @@ -86,4 +86,22 @@ void rejoinAndLogin() { assertThat(member.isPresent()).isTrue(); assertThat(member.get().getMemberStatus()).isEqualTo(MemberStatus.ACTIVE); } + + @DisplayName("최초 애플 로그인인 경우에 DB에 이미 로그인 이력이 있다면 바로 로그인한다.") + @Test + void loginIfExistsMember() { + // given + Member anna = MemberFixture.getAnna(identifier); + memberRepository.save(anna); + + // when + LoginResponse response = appleAuthService.login("idToken", "user"); + + // then + assertThat(response.accessToken()).isNotNull(); + Optional member = memberRepository.findByLoginDetail_Identifier(identifier); + assertThat(member.isPresent()).isTrue(); + assertThat(member.get().getMemberStatus()).isEqualTo(MemberStatus.ACTIVE); + assertThat(member.get().getIdentifier()).isEqualTo(identifier); + } }