-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
[Refactor] #264 - 유저 소셜 로그인 전략 패턴 적용
- Loading branch information
Showing
9 changed files
with
263 additions
and
118 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
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
93 changes: 93 additions & 0 deletions
93
moonshot-api/src/main/java/org/moonshot/user/service/social/GoogleLoginStrategy.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 |
---|---|---|
@@ -0,0 +1,93 @@ | ||
package org.moonshot.user.service.social; | ||
|
||
import static org.moonshot.user.service.validator.UserValidator.isNewUser; | ||
|
||
import java.time.LocalDateTime; | ||
import java.util.Optional; | ||
import lombok.RequiredArgsConstructor; | ||
import org.moonshot.discord.SignUpEvent; | ||
import org.moonshot.jwt.JwtTokenProvider; | ||
import org.moonshot.jwt.TokenResponse; | ||
import org.moonshot.openfeign.dto.response.google.GoogleInfoResponse; | ||
import org.moonshot.openfeign.dto.response.google.GoogleTokenResponse; | ||
import org.moonshot.openfeign.google.GoogleApiClient; | ||
import org.moonshot.openfeign.google.GoogleAuthApiClient; | ||
import org.moonshot.user.dto.request.SocialLoginRequest; | ||
import org.moonshot.user.dto.response.SocialLoginResponse; | ||
import org.moonshot.user.model.User; | ||
import org.moonshot.user.repository.UserRepository; | ||
import org.springframework.beans.factory.annotation.Value; | ||
import org.springframework.context.ApplicationEventPublisher; | ||
import org.springframework.stereotype.Service; | ||
import org.springframework.transaction.annotation.Propagation; | ||
import org.springframework.transaction.annotation.Transactional; | ||
|
||
@Service | ||
@RequiredArgsConstructor | ||
public class GoogleLoginStrategy implements SocialLoginStrategy { | ||
|
||
@Value("${google.client-id}") | ||
private String googleClientId; | ||
|
||
@Value("${google.client-secret}") | ||
private String googleClientSecret; | ||
|
||
@Value("${google.redirect-url}") | ||
private String googleRedirectUrl; | ||
|
||
private final GoogleAuthApiClient googleAuthApiClient; | ||
private final GoogleApiClient googleApiClient; | ||
|
||
private final ApplicationEventPublisher eventPublisher; | ||
private final JwtTokenProvider jwtTokenProvider; | ||
private final UserRepository userRepository; | ||
|
||
@Override | ||
@Transactional | ||
public SocialLoginResponse login(SocialLoginRequest request) { | ||
GoogleTokenResponse tokenResponse = googleAuthApiClient.googleAuth( | ||
request.code(), | ||
googleClientId, | ||
googleClientSecret, | ||
googleRedirectUrl, | ||
"authorization_code" | ||
); | ||
GoogleInfoResponse userResponse = googleApiClient.googleInfo("Bearer " + tokenResponse.accessToken()); | ||
Optional<User> findUser = userRepository.findUserBySocialId(userResponse.sub()); | ||
User user; | ||
if (isNewUser(findUser)) { | ||
User newUser = userRepository.save(User.builderWithSignIn() | ||
.socialId(userResponse.sub()) | ||
.socialPlatform(request.socialPlatform()) | ||
.name(userResponse.name()) | ||
.imageUrl(userResponse.picture()) | ||
.email(userResponse.email()) | ||
.build()); | ||
user = newUser; | ||
publishSignUpEvent(newUser); | ||
} else { | ||
user = findUser.get(); | ||
user.resetDeleteAt(); | ||
} | ||
TokenResponse token = new TokenResponse(jwtTokenProvider.generateAccessToken(user.getId()), jwtTokenProvider.generateRefreshToken(user.getId())); | ||
return SocialLoginResponse.of(user.getId(), user.getName(), token); | ||
} | ||
|
||
@Override | ||
public boolean support(String provider) { | ||
return provider.equals("GOOGLE"); | ||
} | ||
|
||
@Override | ||
@Transactional(propagation = Propagation.REQUIRES_NEW) | ||
public void publishSignUpEvent(User user) { | ||
eventPublisher.publishEvent(SignUpEvent.of( | ||
user.getName(), | ||
user.getEmail() == null ? "" : user.getEmail(), | ||
user.getSocialPlatform().toString(), | ||
LocalDateTime.now(), | ||
user.getImageUrl() | ||
)); | ||
} | ||
|
||
} |
92 changes: 92 additions & 0 deletions
92
moonshot-api/src/main/java/org/moonshot/user/service/social/KakaoLoginStrategy.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 |
---|---|---|
@@ -0,0 +1,92 @@ | ||
package org.moonshot.user.service.social; | ||
|
||
import static org.moonshot.user.service.validator.UserValidator.isNewUser; | ||
import static org.moonshot.util.MDCUtil.USER_REQUEST_ORIGIN; | ||
import static org.moonshot.util.MDCUtil.get; | ||
|
||
import java.time.LocalDateTime; | ||
import java.util.Optional; | ||
import lombok.RequiredArgsConstructor; | ||
import org.moonshot.discord.SignUpEvent; | ||
import org.moonshot.jwt.JwtTokenProvider; | ||
import org.moonshot.jwt.TokenResponse; | ||
import org.moonshot.openfeign.dto.response.kakao.KakaoTokenResponse; | ||
import org.moonshot.openfeign.dto.response.kakao.KakaoUserResponse; | ||
import org.moonshot.openfeign.kakao.KakaoApiClient; | ||
import org.moonshot.openfeign.kakao.KakaoAuthApiClient; | ||
import org.moonshot.user.dto.request.SocialLoginRequest; | ||
import org.moonshot.user.dto.response.SocialLoginResponse; | ||
import org.moonshot.user.model.User; | ||
import org.moonshot.user.repository.UserRepository; | ||
import org.springframework.beans.factory.annotation.Value; | ||
import org.springframework.context.ApplicationEventPublisher; | ||
import org.springframework.stereotype.Service; | ||
import org.springframework.transaction.annotation.Propagation; | ||
import org.springframework.transaction.annotation.Transactional; | ||
|
||
@Service | ||
@RequiredArgsConstructor | ||
public class KakaoLoginStrategy implements SocialLoginStrategy { | ||
|
||
@Value("${kakao.client-id}") | ||
private String kakaoClientId; | ||
|
||
@Value("${kakao.redirect-uri}") | ||
private String kakaoRedirectUri; | ||
|
||
private final KakaoAuthApiClient kakaoAuthApiClient; | ||
private final KakaoApiClient kakaoApiClient; | ||
|
||
private final ApplicationEventPublisher eventPublisher; | ||
private final JwtTokenProvider jwtTokenProvider; | ||
private final UserRepository userRepository; | ||
|
||
@Override | ||
@Transactional | ||
public SocialLoginResponse login(SocialLoginRequest request) { | ||
KakaoTokenResponse tokenResponse = kakaoAuthApiClient.getOAuth2AccessToken( | ||
"authorization_code", | ||
kakaoClientId, | ||
(String)get(USER_REQUEST_ORIGIN) + kakaoRedirectUri, | ||
request.code() | ||
); | ||
KakaoUserResponse userResponse = kakaoApiClient.getUserInformation( | ||
"Bearer " + tokenResponse.accessToken()); | ||
Optional<User> findUser = userRepository.findUserBySocialId(userResponse.id()); | ||
User user; | ||
if (isNewUser(findUser)) { | ||
User newUser = userRepository.save(User.builderWithSignIn() | ||
.socialId(userResponse.id()) | ||
.socialPlatform(request.socialPlatform()) | ||
.name(userResponse.kakaoAccount().profile().nickname()) | ||
.imageUrl(userResponse.kakaoAccount().profile().profileImageUrl()) | ||
.email(null) | ||
.build()); | ||
user = newUser; | ||
publishSignUpEvent(newUser); | ||
} else { | ||
user = findUser.get(); | ||
user.resetDeleteAt(); | ||
} | ||
TokenResponse token = new TokenResponse(jwtTokenProvider.generateAccessToken(user.getId()), jwtTokenProvider.generateRefreshToken(user.getId())); | ||
return SocialLoginResponse.of(user.getId(), user.getName(), token); | ||
} | ||
|
||
@Override | ||
public boolean support(String provider) { | ||
return provider.equals("KAKAO"); | ||
} | ||
|
||
@Override | ||
@Transactional(propagation = Propagation.REQUIRES_NEW) | ||
public void publishSignUpEvent(User user) { | ||
eventPublisher.publishEvent(SignUpEvent.of( | ||
user.getName(), | ||
user.getEmail() == null ? "" : user.getEmail(), | ||
user.getSocialPlatform().toString(), | ||
LocalDateTime.now(), | ||
user.getImageUrl() | ||
)); | ||
} | ||
|
||
} |
Oops, something went wrong.