Skip to content

Commit

Permalink
refact : loadUser 메서드 수정
Browse files Browse the repository at this point in the history
  • Loading branch information
GaBaljaintheroom committed Jan 11, 2024
1 parent 13960f6 commit 4c602de
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,15 @@
import java.util.Map;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.oauth2.core.user.DefaultOAuth2User;
import site.timecapsulearchive.core.domain.auth.entity.Role;

public class CustomOAuth2User extends DefaultOAuth2User {

private final String email;
private final Role role;

public CustomOAuth2User(
Collection<? extends GrantedAuthority> authorities,
Map<String, Object> attributes, String nameAttributeKey, String email, Role role) {
Map<String, Object> attributes, String nameAttributeKey, String email) {
super(authorities, attributes, nameAttributeKey);
this.email = email;
this.role = role;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import lombok.AccessLevel;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.oauth2.client.userinfo.DefaultOAuth2UserService;
import org.springframework.security.oauth2.client.userinfo.OAuth2UserRequest;
import org.springframework.security.oauth2.client.userinfo.OAuth2UserService;
Expand Down Expand Up @@ -46,27 +45,27 @@ public OAuth2User loadUser(OAuth2UserRequest userRequest) throws OAuth2Authentic
Member createMember = getMember(socialType, extractAttributes);

return new CustomOAuth2User(
Collections.singleton(new SimpleGrantedAuthority(createMember.getRole().getKey())),
Collections.emptyList(),
attributes,
extractAttributes.getNameAttributeKey(),
createMember.getEmail(),
createMember.getRole());
createMember.getEmail()
);
}


private Member getMember(SocialType socialType, OAuthAttributes attributes) {
Member findMember = memberRepository.findBySocialTypeAndEmail(socialType,
attributes.getOauth2UserInfo().getEmail()).orElse(null);
if (findMember == null) {
return saveMember(socialType, attributes);
}

return findMember;
return memberRepository.findBySocialTypeAndEmail(
socialType,
attributes.getOauth2UserInfo().getEmail()
)
.orElseGet(() -> saveMember(socialType, attributes));
}

private Member saveMember(SocialType socialType, OAuthAttributes attributes) {
Member createMember = memberMapper.OAuthToEntity(socialType,
attributes.getOauth2UserInfo());
Member createMember = memberMapper.OAuthToEntity(
socialType,
attributes.getOauth2UserInfo()
);
return memberRepository.save(createMember);
}
}

0 comments on commit 4c602de

Please sign in to comment.