-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
16 changed files
with
301 additions
and
297 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
148 changes: 74 additions & 74 deletions
148
src/main/java/com/ttubeog/domain/auth/application/CustomDefaultOAuth2UserService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,74 +1,74 @@ | ||
package com.ttubeog.domain.auth.application; | ||
|
||
import com.ttubeog.domain.member.domain.Provider; | ||
import com.ttubeog.domain.member.domain.Role; | ||
import com.ttubeog.domain.member.domain.Member; | ||
import com.ttubeog.domain.member.domain.repository.MemberRepository; | ||
import com.ttubeog.global.DefaultAssert; | ||
import com.ttubeog.global.config.security.auth.OAuth2UserInfo; | ||
import com.ttubeog.global.config.security.auth.OAuth2UserInfoFactory; | ||
import com.ttubeog.global.config.security.token.MemberPrincipal; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.security.oauth2.client.userinfo.DefaultOAuth2UserService; | ||
import org.springframework.security.oauth2.client.userinfo.OAuth2UserRequest; | ||
import org.springframework.security.oauth2.core.OAuth2AuthenticationException; | ||
import org.springframework.security.oauth2.core.user.OAuth2User; | ||
import org.springframework.stereotype.Service; | ||
|
||
import java.util.Optional; | ||
|
||
@RequiredArgsConstructor | ||
@Service | ||
public class CustomDefaultOAuth2UserService extends DefaultOAuth2UserService{ | ||
|
||
private final MemberRepository memberRepository; | ||
|
||
@Override | ||
public OAuth2User loadUser(OAuth2UserRequest oAuth2UserRequest) throws OAuth2AuthenticationException { | ||
OAuth2User oAuth2User = super.loadUser(oAuth2UserRequest); | ||
try { | ||
return processOAuth2User(oAuth2UserRequest, oAuth2User); | ||
} catch (Exception e) { | ||
DefaultAssert.isAuthentication(e.getMessage()); | ||
} | ||
return null; | ||
} | ||
|
||
private OAuth2User processOAuth2User(OAuth2UserRequest oAuth2UserRequest, OAuth2User oAuth2User) { | ||
OAuth2UserInfo oAuth2UserInfo = OAuth2UserInfoFactory.getOAuth2UserInfo(oAuth2UserRequest.getClientRegistration().getRegistrationId(), oAuth2User.getAttributes()); | ||
DefaultAssert.isAuthentication(!oAuth2UserInfo.getEmail().isEmpty()); | ||
|
||
Optional<Member> userOptional = memberRepository.findByEmail(oAuth2UserInfo.getEmail()); | ||
Member member; | ||
if(userOptional.isPresent()) { | ||
member = userOptional.get(); | ||
DefaultAssert.isAuthentication(member.getProvider().equals(Provider.valueOf(oAuth2UserRequest.getClientRegistration().getRegistrationId()))); | ||
member = updateExistingMember(member, oAuth2UserInfo); | ||
} else { | ||
member = registerNewMember(oAuth2UserRequest, oAuth2UserInfo); | ||
} | ||
|
||
return MemberPrincipal.create(member, oAuth2User.getAttributes()); | ||
} | ||
|
||
private Member registerNewMember(OAuth2UserRequest oAuth2UserRequest, OAuth2UserInfo oAuth2UserInfo) { | ||
Member member = Member.builder() | ||
.provider(Provider.valueOf(oAuth2UserRequest.getClientRegistration().getRegistrationId())) | ||
.providerId(oAuth2UserInfo.getId()) | ||
.name(oAuth2UserInfo.getName()) | ||
.email(oAuth2UserInfo.getEmail()) | ||
.imageUrl(oAuth2UserInfo.getImageUrl()) | ||
.role(Role.USER) | ||
.build(); | ||
return memberRepository.save(member); | ||
} | ||
|
||
private Member updateExistingMember(Member member, OAuth2UserInfo oAuth2UserInfo) { | ||
|
||
member.updateName(oAuth2UserInfo.getName()); | ||
member.updateImageUrl(oAuth2UserInfo.getImageUrl()); | ||
|
||
return memberRepository.save(member); | ||
} | ||
} | ||
//package com.ttubeog.domain.auth.application; | ||
// | ||
//import com.ttubeog.domain.member.domain.Provider; | ||
//import com.ttubeog.domain.member.domain.Role; | ||
//import com.ttubeog.domain.member.domain.Member; | ||
//import com.ttubeog.domain.member.domain.repository.MemberRepository; | ||
//import com.ttubeog.global.DefaultAssert; | ||
//import com.ttubeog.global.config.security.auth.OAuth2UserInfo; | ||
//import com.ttubeog.global.config.security.token.UserPrincipal; | ||
//import lombok.RequiredArgsConstructor; | ||
//import org.springframework.security.oauth2.client.userinfo.DefaultOAuth2UserService; | ||
//import org.springframework.security.oauth2.client.userinfo.OAuth2UserRequest; | ||
//import org.springframework.security.oauth2.core.OAuth2AuthenticationException; | ||
//import org.springframework.security.oauth2.core.user.OAuth2User; | ||
//import org.springframework.stereotype.Service; | ||
// | ||
//import java.util.Optional; | ||
// | ||
//@RequiredArgsConstructor | ||
//@Service | ||
//public class CustomDefaultOAuth2UserService extends DefaultOAuth2UserService { | ||
// | ||
// private final MemberRepository memberRepository; | ||
// | ||
// @Override | ||
// public OAuth2User loadUser(OAuth2UserRequest oAuth2UserRequest) throws OAuth2AuthenticationException { | ||
// OAuth2User oAuth2User = super.loadUser(oAuth2UserRequest); | ||
// try { | ||
// return processOAuth2User(oAuth2UserRequest, oAuth2User); | ||
// } catch (Exception e) { | ||
// DefaultAssert.isAuthentication(e.getMessage()); | ||
// } | ||
// return null; | ||
// } | ||
// | ||
// private OAuth2User processOAuth2User(OAuth2UserRequest oAuth2UserRequest, OAuth2User oAuth2User) { | ||
// OAuth2UserInfo oAuth2UserInfo = OAuth2UserInfoFactory.getOAuth2UserInfo(oAuth2UserRequest.getClientRegistration().getRegistrationId(), oAuth2User.getAttributes()); | ||
// DefaultAssert.isAuthentication(!oAuth2UserInfo.getEmail().isEmpty()); | ||
// | ||
// Optional<Member> userOptional = memberRepository.findByEmail(oAuth2UserInfo.getEmail()); | ||
// Member member; | ||
// if (userOptional.isPresent()) { | ||
// member = userOptional.get(); | ||
// DefaultAssert.isAuthentication(member.getProvider().equals(Provider.valueOf(oAuth2UserRequest.getClientRegistration().getRegistrationId()))); | ||
// member = updateExistingMember(member, oAuth2UserInfo); | ||
// } else { | ||
// member = registerNewMember(oAuth2UserRequest, oAuth2UserInfo); | ||
// } | ||
// | ||
// return UserPrincipal.create(member, oAuth2User.getAttributes()); | ||
// } | ||
// | ||
// private Member registerNewMember(OAuth2UserRequest oAuth2UserRequest, OAuth2UserInfo oAuth2UserInfo) { | ||
// Member member = Member.builder() | ||
// .provider(Provider.valueOf(oAuth2UserRequest.getClientRegistration().getRegistrationId())) | ||
// .providerId(oAuth2UserInfo.getId()) | ||
// .name(oAuth2UserInfo.getName()) | ||
// .email(oAuth2UserInfo.getEmail()) | ||
// .imageUrl(oAuth2UserInfo.getImageUrl()) | ||
// .refreshToken(oAuth2UserInfo.getRefreshToken()) | ||
// .role(Role.USER) | ||
// .build(); | ||
// | ||
// return memberRepository.save(member); | ||
// } | ||
// | ||
// private Member updateExistingMember(Member member, OAuth2UserInfo oAuth2UserInfo) { | ||
// | ||
// member.updateName(oAuth2UserInfo.getName()); | ||
// member.updateImageUrl(oAuth2UserInfo.getImageUrl()); | ||
// | ||
// return memberRepository.save(member); | ||
// } | ||
//} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
package com.ttubeog.domain.member.domain; | ||
|
||
public enum Provider { | ||
kakao, | ||
apple | ||
KAKAO, | ||
APPLE | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.