From 4c602decf3d28ef64d41add0377e5d8f871ddd37 Mon Sep 17 00:00:00 2001 From: GaBaljaintheroom Date: Thu, 11 Jan 2024 20:45:00 +0900 Subject: [PATCH] =?UTF-8?q?refact=20:=20loadUser=20=EB=A9=94=EC=84=9C?= =?UTF-8?q?=EB=93=9C=20=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../auth/dto/oauth/CustomOAuth2User.java | 5 +--- .../auth/service/CustomOAuth2UserService.java | 25 +++++++++---------- 2 files changed, 13 insertions(+), 17 deletions(-) diff --git a/backend/core/src/main/java/site/timecapsulearchive/core/domain/auth/dto/oauth/CustomOAuth2User.java b/backend/core/src/main/java/site/timecapsulearchive/core/domain/auth/dto/oauth/CustomOAuth2User.java index 9bd4c88a1..7f41276c8 100644 --- a/backend/core/src/main/java/site/timecapsulearchive/core/domain/auth/dto/oauth/CustomOAuth2User.java +++ b/backend/core/src/main/java/site/timecapsulearchive/core/domain/auth/dto/oauth/CustomOAuth2User.java @@ -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 authorities, - Map attributes, String nameAttributeKey, String email, Role role) { + Map attributes, String nameAttributeKey, String email) { super(authorities, attributes, nameAttributeKey); this.email = email; - this.role = role; } } diff --git a/backend/core/src/main/java/site/timecapsulearchive/core/domain/auth/service/CustomOAuth2UserService.java b/backend/core/src/main/java/site/timecapsulearchive/core/domain/auth/service/CustomOAuth2UserService.java index bf015d185..ed4f5198e 100644 --- a/backend/core/src/main/java/site/timecapsulearchive/core/domain/auth/service/CustomOAuth2UserService.java +++ b/backend/core/src/main/java/site/timecapsulearchive/core/domain/auth/service/CustomOAuth2UserService.java @@ -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; @@ -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); } }