Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor: 사용자 소셜 가입 정보 User 도메인 통합 #90

Merged
merged 1 commit into from
Sep 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import static slvtwn.khu.toyouserver.domain.SocialAuthProvider.KAKAO;

import java.util.Optional;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
Expand All @@ -11,20 +10,16 @@
import slvtwn.khu.toyouserver.common.authentication.jwt.Token;
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.UserSocialAccount;
import slvtwn.khu.toyouserver.dto.SocialAuthRequest;
import slvtwn.khu.toyouserver.dto.SocialAuthResponse;
import slvtwn.khu.toyouserver.persistance.UserRepository;
import slvtwn.khu.toyouserver.persistance.UserSocialAccountRepository;

@Service
@RequiredArgsConstructor
public class KakaoAuthStrategy implements SocialAuthStrategy {

private final UserSocialAccountRepository userSocialAccountRepository;
@Value("${oauth.kakao.client-id}")
private String kakaoClientId;
@Value("${oauth.kakao.redirect-uri}")
Expand All @@ -44,14 +39,17 @@ public class KakaoAuthStrategy implements SocialAuthStrategy {
@Override
@Transactional
public SocialAuthResponse login(SocialAuthRequest request) {
KakaoTokenResponse tokenResponse = kakaoAuthApiClient.getOAuth2AccessToken(
kakaoGrantType,
kakaoClientId,
kakaoRedirectUri,
request.authorizationCode()
);
// KakaoTokenResponse tokenResponse = kakaoAuthApiClient.getOAuth2AccessToken(
// kakaoGrantType,
// kakaoClientId,
// kakaoRedirectUri,
// request.authorizationCode()
// );
// KakaoUserResponse userResponse = kakaoResourceApiClient.getUserInformation(
// "Bearer " + tokenResponse.accessToken());

KakaoUserResponse userResponse = kakaoResourceApiClient.getUserInformation(
"Bearer " + tokenResponse.accessToken());
"Bearer " + "5KPI3VXXHURq8oW-Tf4QakyEigvBgSNoAAAAAQo8JCAAAAGSQcoJl-AsyCcGfplL");
User user = findOrCreateUser(userResponse);
Token token = jwtProvider.issueTokens(user.getId());
return SocialAuthResponse.of(user.getId(), user.getName(), KAKAO, token);
Expand All @@ -63,26 +61,18 @@ public boolean support(String provider) {
}

private User findOrCreateUser(KakaoUserResponse userResponse) {
Optional<UserSocialAccount> account = userSocialAccountRepository.findByProviderSerial(
userResponse.id());
if (account.isPresent()) {
UserSocialAccount verifiedSocialAccount = account.get();
return userRepository.findById(verifiedSocialAccount.getUserId()).orElseThrow(
() -> new IllegalStateException("등록된 카카오 계정에 해당하는 유저가 없습니다."));
} else {
return registerUser(userResponse);
}
return userRepository.findByProviderSerial(userResponse.id())
.orElseGet(() -> registerUser(userResponse));
}

private User registerUser(KakaoUserResponse userResponse) {
User user = User.create(
userResponse.kakaoAccount().profile().nickname(),
userResponse.kakaoAccount().profile().profileImageUrl(),
KAKAO
KAKAO,
userResponse.id()
);
userRepository.save(user);
UserSocialAccount newAccount = UserSocialAccount.createAccountByKakao(user.getId(), userResponse.id());
userSocialAccountRepository.save(newAccount);
return user;
}
}
11 changes: 8 additions & 3 deletions src/main/java/slvtwn/khu/toyouserver/domain/User.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,15 @@
import jakarta.persistence.Table;
import java.time.LocalDate;
import lombok.AccessLevel;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import slvtwn.khu.toyouserver.common.entity.BaseTimeEntity;

@Entity
@Table(name = "users")
@NoArgsConstructor(access = AccessLevel.PROTECTED)
@AllArgsConstructor(access = AccessLevel.PRIVATE)
@Getter
public class User extends BaseTimeEntity {

Expand All @@ -34,15 +36,18 @@ public class User extends BaseTimeEntity {
@Enumerated(EnumType.STRING)
private SocialAuthProvider provider;

public User(String name, LocalDate birthday, String introduction, String profilePicture, SocialAuthProvider provider) {
private String providerSerial;

public User(String name, LocalDate birthday, String introduction, String profilePicture,
SocialAuthProvider provider) {
this.name = name;
this.birthday = birthday;
this.introduction = introduction;
this.profilePicture = profilePicture;
}

public static User create(String name, String profilePicture, SocialAuthProvider provider) {
return new User(name, null, null, profilePicture, provider);
public static User create(String name, String profilePicture, SocialAuthProvider provider, String providerSerial) {
return new User(null, name, null, null, profilePicture, provider, providerSerial);
}

public User updateInfo(String name, LocalDate birthday, String introduction, String profilePicture) {
Expand Down
36 changes: 0 additions & 36 deletions src/main/java/slvtwn/khu/toyouserver/domain/UserSocialAccount.java

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
package slvtwn.khu.toyouserver.persistance;

import java.util.Optional;
import org.springframework.data.jpa.repository.JpaRepository;
import slvtwn.khu.toyouserver.domain.User;

public interface UserRepository extends JpaRepository<User, Long> {

Optional<User> findByProviderSerial(String providerSerial);
}

This file was deleted.