Skip to content

Commit

Permalink
Merge pull request #90 from Princess-in-silvertown/refactor/88
Browse files Browse the repository at this point in the history
Refactor: ์‚ฌ์šฉ์ž ์†Œ์…œ ๊ฐ€์ž… ์ •๋ณด User ๋„๋ฉ”์ธ ํ†ตํ•ฉ
  • Loading branch information
loveysuby authored Sep 30, 2024
2 parents 7a5208f + 65f2072 commit 6661f21
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 73 deletions.
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.

0 comments on commit 6661f21

Please sign in to comment.