Skip to content

Commit

Permalink
refactor: LoginMemberDto에 Member객체가 아닌 필드를 담도록 수정
Browse files Browse the repository at this point in the history
  • Loading branch information
xxeol2 committed Sep 11, 2023
1 parent 1f9dce1 commit dbe9b56
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
import com.festago.auth.dto.LoginMemberDto;
import com.festago.auth.dto.LoginRequest;
import com.festago.auth.dto.LoginResponse;
import com.festago.domain.Member;
import org.springframework.stereotype.Service;

@Service
Expand All @@ -28,13 +27,12 @@ public AuthFacadeService(AuthService authService, OAuth2Clients oAuth2Clients,

public LoginResponse login(LoginRequest request) {
LoginMemberDto loginMember = authService.login(getUserInfo(request));
Member member = loginMember.member();
String accessToken = getAccessToken(member);
String accessToken = getAccessToken(loginMember.memberId());
return LoginResponse.of(accessToken, loginMember);
}

private String getAccessToken(Member member) {
return authProvider.provide(new AuthPayload(member.getId(), Role.MEMBER));
private String getAccessToken(Long memberId) {
return authProvider.provide(new AuthPayload(memberId, Role.MEMBER));
}

private UserInfo getUserInfo(LoginRequest request) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
import com.festago.domain.MemberRepository;
import com.festago.exception.ErrorCode;
import com.festago.exception.NotFoundException;
import java.util.Optional;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

Expand All @@ -21,13 +20,12 @@ public AuthService(MemberRepository memberRepository) {
}

public LoginMemberDto login(UserInfo userInfo) {
Optional<Member> optionalMember = memberRepository.findBySocialIdAndSocialType(userInfo.socialId(),
userInfo.socialType());
if (optionalMember.isPresent()) {
return LoginMemberDto.isExists(optionalMember.get());
}
Member member = signUp(userInfo);
return LoginMemberDto.isNew(member);
return memberRepository.findBySocialIdAndSocialType(userInfo.socialId(), userInfo.socialType())
.map(LoginMemberDto::isExists)
.orElseGet(() -> {
Member member = signUp(userInfo);
return LoginMemberDto.isNew(member);
});
}

private Member signUp(UserInfo userInfo) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,15 @@

public record LoginMemberDto(
boolean isNew,
Member member
Long memberId,
String nickname
) {

public static LoginMemberDto isNew(Member member) {
return new LoginMemberDto(true, member);
return new LoginMemberDto(true, member.getId(), member.getNickname());
}

public static LoginMemberDto isExists(Member member) {
return new LoginMemberDto(false, member);
return new LoginMemberDto(false, member.getId(), member.getNickname());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@ public record LoginResponse(
) {

public static LoginResponse of(String accessToken, LoginMemberDto loginMember) {
return new LoginResponse(accessToken, loginMember.member().getNickname(), loginMember.isNew());
return new LoginResponse(accessToken, loginMember.nickname(), loginMember.isNew());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ void setUp() {
.willReturn("Bearer token");

given(authService.login(any(UserInfo.class)))
.willReturn(new LoginMemberDto(false, member));
.willReturn(new LoginMemberDto(false, member.getId(), member.getNickname()));

// when
LoginResponse response = authFacadeService.login(request);
Expand Down

0 comments on commit dbe9b56

Please sign in to comment.