Skip to content

Commit

Permalink
Merge pull request #739 from woowacourse-teams/fix/#738
Browse files Browse the repository at this point in the history
애플 로그인 시 이미 가입한 유저라면 회원가입 실패 기능 구현
  • Loading branch information
Mingyum-Kim authored Oct 23, 2024
2 parents 69e3fd5 + 682726d commit 38caff3
Show file tree
Hide file tree
Showing 7 changed files with 35 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package mouda.backend.auth.business;

import java.util.Optional;

import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Service;

Expand Down Expand Up @@ -32,6 +34,10 @@ public LoginResponse login(String idToken, String user) {
}

private LoginResponse handleNewUser(String user, String identifier) {
Optional<Member> 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());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package mouda.backend.member.implement;

import java.util.Optional;

import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Component;

Expand All @@ -15,6 +17,10 @@ public class MemberFinder {

private final MemberRepository memberRepository;

public Optional<Member> 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));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@

public interface MemberRepository extends JpaRepository<Member, Long> {

Optional<Member> findByLoginDetail_Identifier(String identifier);

@Query("""
SELECT m FROM Member m
WHERE m.loginDetail.identifier = :identifier AND (m.memberStatus = 'ACTIVE' OR m.memberStatus = 'DELETED')
Expand Down
2 changes: 1 addition & 1 deletion backend/src/main/resources/application-dev.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
1 change: 1 addition & 0 deletions backend/src/main/resources/application-prod.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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> member = memberRepository.findByLoginDetail_Identifier(identifier);
assertThat(member.isPresent()).isTrue();
assertThat(member.get().getMemberStatus()).isEqualTo(MemberStatus.ACTIVE);
assertThat(member.get().getIdentifier()).isEqualTo(identifier);
}
}

0 comments on commit 38caff3

Please sign in to comment.