-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
✨ feat: KAKAO OAuth 사용자 가입 로직 구현 (#42)
- Loading branch information
Showing
5 changed files
with
173 additions
and
1 deletion.
There are no files selected for viewing
47 changes: 47 additions & 0 deletions
47
src/main/java/slvtwn/khu/toyouserver/application/auth/SocialAuthContext.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,47 @@ | ||
package slvtwn.khu.toyouserver.application.auth; | ||
|
||
import static slvtwn.khu.toyouserver.common.response.ResponseType.NOT_SUPPORTED_AUTH_PROVIDER; | ||
|
||
import jakarta.annotation.PostConstruct; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.stereotype.Component; | ||
import slvtwn.khu.toyouserver.application.auth.strategy.KakaoAuthStrategy; | ||
import slvtwn.khu.toyouserver.application.auth.strategy.SocialAuthStrategy; | ||
import slvtwn.khu.toyouserver.domain.SocialAuthProvider; | ||
import slvtwn.khu.toyouserver.dto.SocialAuthRequest; | ||
import slvtwn.khu.toyouserver.dto.SocialAuthResponse; | ||
import slvtwn.khu.toyouserver.exception.ToyouException; | ||
|
||
@Component | ||
@RequiredArgsConstructor | ||
public class SocialAuthContext { | ||
|
||
private KakaoAuthStrategy kakaoAuthStrategy; | ||
|
||
private final List<SocialAuthStrategy> socialAuthStrategies = new ArrayList<>(); | ||
|
||
@PostConstruct | ||
void initSocialLoginContext() { | ||
socialAuthStrategies.add(kakaoAuthStrategy); | ||
} | ||
|
||
public boolean support(SocialAuthProvider provider) { | ||
for (SocialAuthStrategy strategy : socialAuthStrategies) { | ||
if (strategy.support(provider.toString())) { | ||
return true; | ||
} | ||
} | ||
return false; | ||
} | ||
|
||
public SocialAuthResponse doLogin(final SocialAuthRequest request) { | ||
for (SocialAuthStrategy strategy : socialAuthStrategies) { | ||
if (strategy.support(request.provider().toString())) { | ||
return strategy.login(request); | ||
} | ||
} | ||
throw new ToyouException(NOT_SUPPORTED_AUTH_PROVIDER); | ||
} | ||
} |
81 changes: 81 additions & 0 deletions
81
src/main/java/slvtwn/khu/toyouserver/application/auth/strategy/KakaoAuthStrategy.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,81 @@ | ||
package slvtwn.khu.toyouserver.application.auth.strategy; | ||
|
||
import static slvtwn.khu.toyouserver.domain.SocialAuthProvider.KAKAO; | ||
|
||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.beans.factory.annotation.Value; | ||
import org.springframework.stereotype.Service; | ||
import org.springframework.transaction.annotation.Transactional; | ||
import slvtwn.khu.toyouserver.application.AuthService; | ||
import slvtwn.khu.toyouserver.common.feign.auth.kakao.KakaoAuthApiClient; | ||
import slvtwn.khu.toyouserver.common.feign.auth.kakao.KakaoResourceApiClient; | ||
import slvtwn.khu.toyouserver.common.feign.auth.kakao.web.KakaoTokenResponse; | ||
import slvtwn.khu.toyouserver.common.feign.auth.kakao.web.KakaoUserResponse; | ||
import slvtwn.khu.toyouserver.domain.User; | ||
import slvtwn.khu.toyouserver.domain.UserOAuthCredential; | ||
import slvtwn.khu.toyouserver.domain.UserOAuthCredentialRepository; | ||
import slvtwn.khu.toyouserver.dto.SocialAuthRequest; | ||
import slvtwn.khu.toyouserver.dto.SocialAuthResponse; | ||
import slvtwn.khu.toyouserver.dto.TokenResponse; | ||
import slvtwn.khu.toyouserver.persistance.UserRepository; | ||
|
||
@Service | ||
@RequiredArgsConstructor | ||
public class KakaoAuthStrategy implements SocialAuthStrategy { | ||
|
||
private final AuthService authService; | ||
@Value("${oauth.kakao.client-id}") | ||
private String kakaoClientId; | ||
@Value("${oauth.kakao.redirect-uri}") | ||
private String kakaoRedirectUri; | ||
@Value("${oauth.kakao.grant-type}") | ||
private String grantType; | ||
|
||
private final KakaoAuthApiClient kakaoAuthApiClient; | ||
private final KakaoResourceApiClient kakaoResourceApiClient; | ||
|
||
private final UserRepository userRepository; | ||
private final UserOAuthCredentialRepository userOAuthCredentialRepository; | ||
|
||
private final JwtTokenProvider jwtTokenProvider; | ||
|
||
@Override | ||
@Transactional | ||
public SocialAuthResponse login(SocialAuthRequest request) { | ||
KakaoTokenResponse tokenResponse = kakaoAuthApiClient.getOAuth2AccessToken( | ||
grantType, | ||
kakaoClientId, | ||
kakaoRedirectUri, | ||
request.authorizationCode() | ||
); | ||
KakaoUserResponse userResponse = kakaoResourceApiClient.getUserInformation( | ||
"Bearer " + tokenResponse.accessToken()); | ||
User user = handleUserOAuthCredential(userResponse); | ||
TokenResponse token = new TokenResponse(jwtTokenProvider.generateAccessToken(user.getId()), | ||
jwtTokenProvider.generateRefreshToken(user.getId())); | ||
return SocialAuthResponse.of(user.getId(), user.getName(), KAKAO, token); | ||
} | ||
|
||
@Override | ||
public boolean support(String provider) { | ||
return provider.equals("KAKAO"); | ||
} | ||
|
||
private User handleUserOAuthCredential(KakaoUserResponse userResponse) { | ||
UserOAuthCredential userOAuthCredential = userOAuthCredentialRepository.findBySerialId(userResponse.serialId()) | ||
.orElse(null); | ||
|
||
User authenticatedUser; | ||
if (userOAuthCredential == null) { | ||
authenticatedUser = User.create(userResponse.kakaoAccount().profile().nickname(), | ||
userResponse.kakaoAccount().profile().profileImageUrl()); | ||
userOAuthCredential = UserOAuthCredential.recordKAKAOCredential(authenticatedUser, userResponse.serialId()); | ||
|
||
userRepository.save(authenticatedUser); | ||
userOAuthCredentialRepository.save(userOAuthCredential); | ||
} else { | ||
authenticatedUser = userOAuthCredential.getUser(); | ||
} | ||
return authenticatedUser; | ||
} | ||
} |
10 changes: 10 additions & 0 deletions
10
src/main/java/slvtwn/khu/toyouserver/application/auth/strategy/SocialAuthStrategy.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,10 @@ | ||
package slvtwn.khu.toyouserver.application.auth.strategy; | ||
|
||
import slvtwn.khu.toyouserver.dto.SocialAuthRequest; | ||
import slvtwn.khu.toyouserver.dto.SocialAuthResponse; | ||
|
||
public interface SocialAuthStrategy { | ||
SocialAuthResponse login(final SocialAuthRequest request); | ||
|
||
boolean support(String provider); | ||
} |
28 changes: 28 additions & 0 deletions
28
src/main/java/slvtwn/khu/toyouserver/presentation/AuthController.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,28 @@ | ||
package slvtwn.khu.toyouserver.presentation; | ||
|
||
import static slvtwn.khu.toyouserver.common.response.ResponseType.NOT_SUPPORTED_AUTH_PROVIDER; | ||
|
||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.web.bind.annotation.PostMapping; | ||
import org.springframework.web.bind.annotation.RequestBody; | ||
import org.springframework.web.bind.annotation.RequestMapping; | ||
import slvtwn.khu.toyouserver.application.auth.SocialAuthContext; | ||
import slvtwn.khu.toyouserver.common.response.ToyouResponse; | ||
import slvtwn.khu.toyouserver.dto.SocialAuthRequest; | ||
import slvtwn.khu.toyouserver.dto.SocialAuthResponse; | ||
import slvtwn.khu.toyouserver.exception.ToyouException; | ||
|
||
@RequiredArgsConstructor | ||
@RequestMapping("/auth") | ||
public class AuthController { | ||
|
||
private final SocialAuthContext socialAuthContext; | ||
|
||
@PostMapping("/login") | ||
public ToyouResponse<SocialAuthResponse> login(@RequestBody SocialAuthRequest request) { | ||
if (socialAuthContext.support(request.provider())) { | ||
return ToyouResponse.from(socialAuthContext.doLogin(request)); | ||
} | ||
throw new ToyouException(NOT_SUPPORTED_AUTH_PROVIDER); | ||
} | ||
} |
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