Skip to content

Commit

Permalink
refactor: Apply Single Responsibility Principle (#7)
Browse files Browse the repository at this point in the history
  • Loading branch information
toychip committed Nov 28, 2023
1 parent 045fc04 commit 99233bb
Showing 1 changed file with 19 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import com.api.TaveShot.domain.Member.Member;
import com.api.TaveShot.domain.Member.MemberRepository;
import java.util.Map;
import java.util.Optional;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.oauth2.client.userinfo.DefaultOAuth2UserService;
Expand All @@ -21,37 +20,38 @@ public class CustomOAuth2UserService extends DefaultOAuth2UserService {

@Override
public OAuth2User loadUser(OAuth2UserRequest userRequest) throws OAuth2AuthenticationException {

OAuth2User oAuth2User = super.loadUser(userRequest);
log.info("-------------- 갖고 온 정보 -------------- getAttributes : {} ", oAuth2User.getAuthorities());

// GitHub에서 반환된 사용자 정보에서 이메일 추출
Map<String, Object> attributes = oAuth2User.getAttributes();
Member member = processOAuthUser(attributes);
return createCustomOauth2User(member, oAuth2User);
}

private Member processOAuthUser(Map<String, Object> attributes) {
String gitLoginId = (String) attributes.get("login");
Long gitId = ((Integer) attributes.get("id")).longValue();
String gitEmail = (String) attributes.get("email");
String gitName = (String) attributes.get("name");

Optional<Member> findMember = memberRepository.findByGitId(gitId);

if (findMember.isEmpty()) {
Member newMember = Member.builder()
.gitId(gitId)
.gitLoginId(gitLoginId)
.gitEmail(gitEmail)
.gitName(gitName)
.build();
return memberRepository.findByGitId(gitId)
.orElseGet(() -> registerNewMember(gitId, gitLoginId, gitEmail, gitName));
}

memberRepository.save(newMember);
private Member registerNewMember(Long gitId, String gitLoginId, String gitEmail, String gitName) {
Member newMember = Member.builder()
.gitId(gitId)
.gitLoginId(gitLoginId)
.gitEmail(gitEmail)
.gitName(gitName)
.build();

return CustomOauth2User.builder()
.member(newMember)
.attributes(oAuth2User.getAttributes())
.build();
}
return memberRepository.save(newMember);
}

private CustomOauth2User createCustomOauth2User(Member member, OAuth2User oAuth2User) {
return CustomOauth2User.builder()
.member(findMember.get())
.member(member)
.attributes(oAuth2User.getAttributes())
.build();
}
Expand Down

0 comments on commit 99233bb

Please sign in to comment.