diff --git a/server-yml b/server-yml index a7d6b4d4..45868e19 160000 --- a/server-yml +++ b/server-yml @@ -1 +1 @@ -Subproject commit a7d6b4d403c4c09d3c0db99b1187d0d8bcc1f850 +Subproject commit 45868e19e645aa10b406f507135844a83503fe4b diff --git a/src/main/java/org/hankki/hankkiserver/api/auth/controller/request/UserLoginRequest.java b/src/main/java/org/hankki/hankkiserver/api/auth/controller/request/UserLoginRequest.java index 708c7ebf..7caafcc1 100644 --- a/src/main/java/org/hankki/hankkiserver/api/auth/controller/request/UserLoginRequest.java +++ b/src/main/java/org/hankki/hankkiserver/api/auth/controller/request/UserLoginRequest.java @@ -1,11 +1,10 @@ package org.hankki.hankkiserver.api.auth.controller.request; import jakarta.annotation.Nullable; -import org.hankki.hankkiserver.domain.user.model.Platform; public record UserLoginRequest( @Nullable String name, - Platform platform + String platform ) { } diff --git a/src/main/java/org/hankki/hankkiserver/api/auth/service/AuthService.java b/src/main/java/org/hankki/hankkiserver/api/auth/service/AuthService.java index 526f9912..857828f4 100644 --- a/src/main/java/org/hankki/hankkiserver/api/auth/service/AuthService.java +++ b/src/main/java/org/hankki/hankkiserver/api/auth/service/AuthService.java @@ -8,7 +8,6 @@ import org.hankki.hankkiserver.domain.user.model.User; import org.hankki.hankkiserver.domain.user.model.UserInfo; import org.hankki.hankkiserver.domain.user.model.Platform; -import org.hankki.hankkiserver.common.exception.EntityNotFoundException; import org.hankki.hankkiserver.common.exception.InvalidValueException; import org.hankki.hankkiserver.common.exception.UnauthorizedException; import org.hankki.hankkiserver.external.openfeign.apple.AppleOAuthProvider; @@ -20,7 +19,9 @@ import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; -import static org.hankki.hankkiserver.domain.user.model.MemberStatus.INACTIVE; +import java.util.Optional; + +import static org.hankki.hankkiserver.domain.user.model.MemberStatus.ACTIVE; import static org.hankki.hankkiserver.domain.user.model.Platform.APPLE; import static org.hankki.hankkiserver.domain.user.model.Platform.KAKAO; import static org.hankki.hankkiserver.auth.filter.JwtAuthenticationFilter.BEARER; @@ -30,8 +31,11 @@ @RequiredArgsConstructor public class AuthService { - private final UserAdapter userAdapter; - private final UserInfoAdapter userInfoAdapter; + private final UserFinder userFinder; + private final UserSaver userSaver; + private final UserInfoFinder userInfoFinder; + private final UserInfoSaver userInfoSaver; + private final UserInfoDeleter userInfoDeleter; private final JwtProvider jwtProvider; private final JwtValidator jwtValidator; private final KakaoOAuthProvider kakaoOAuthProvider; @@ -40,21 +44,21 @@ public class AuthService { public UserLoginResponse login( final String token, final UserLoginRequest request) { - Platform platform = request.platform(); + Platform platform = Platform.getEnumPlatformFromStringPlatform(request.platform()); SocialInfoDto socialInfo = getSocialInfo(token, platform, request.name()); - boolean isRegistered = userAdapter.isRegisteredUser(platform, socialInfo); - User findUser = loadOrCreateUser(platform, socialInfo, isRegistered); + boolean isRegistered = userFinder.isRegisteredUser(platform, socialInfo); + User findUser = loadOrCreateUser(platform, socialInfo); Token issuedToken = generateTokens(findUser.getId()); return UserLoginResponse.of(issuedToken, isRegistered); } public void logOut(final long userId) { - UserInfo findUserInfo = userInfoAdapter.getUserInfo(userId); - updateRefreshToken(null, findUserInfo); + UserInfo findUserInfo = userInfoFinder.getUserInfo(userId); + findUserInfo.updateRefreshToken(null); } public void withdraw(final long userId, final String code) { - User user = userAdapter.getUser(userId); + User user = userFinder.getUser(userId); if (APPLE == user.getPlatform()){ try { String refreshToken = appleOAuthProvider.getAppleToken(code); @@ -63,24 +67,24 @@ public void withdraw(final long userId, final String code) { throw new InvalidValueException(ErrorCode.APPLE_REVOKE_FAILED); } } - user.softDelete(INACTIVE); - userInfoAdapter.softDelete(userId); + user.softDelete(); + userInfoDeleter.softDelete(userId); } @Transactional(noRollbackFor = UnauthorizedException.class) public UserReissueResponse reissue(final String refreshToken) { long userId = jwtProvider.getSubject(refreshToken.substring(BEARER.length())); validateRefreshToken(refreshToken, userId); - UserInfo findUserInfo = userInfoAdapter.getUserInfo(userId); - Token issueTokens = jwtProvider.issueTokens(userId); - updateRefreshToken(issueTokens.refreshToken(), findUserInfo); - return UserReissueResponse.of(issueTokens); + UserInfo findUserInfo = userInfoFinder.getUserInfo(userId); + Token issuedTokens = jwtProvider.issueTokens(userId); + findUserInfo.updateRefreshToken(issuedTokens.refreshToken()); + return UserReissueResponse.of(issuedTokens); } private Token generateTokens(final long userId) { Token issuedTokens = jwtProvider.issueTokens(userId); - UserInfo findUserInfo = userInfoAdapter.getUserInfo(userId); - updateRefreshToken(issuedTokens.refreshToken(), findUserInfo); + UserInfo findUserInfo = userInfoFinder.getUserInfo(userId); + findUserInfo.updateRefreshToken(issuedTokens.refreshToken()); return issuedTokens; } @@ -90,14 +94,13 @@ private SocialInfoDto getSocialInfo( final String name) { if (KAKAO == platform){ return kakaoOAuthProvider.getKakaoUserInfo(providerToken); - } else if (APPLE == platform){ - return appleOAuthProvider.getAppleUserInfo(providerToken, name); } - throw new InvalidValueException(ErrorCode.INVALID_PLATFORM_TYPE); + return appleOAuthProvider.getAppleUserInfo(providerToken, name); } - private User loadOrCreateUser(Platform platform, SocialInfoDto socialInfo, boolean isRegistered){ - if (!isRegistered){ + private User loadOrCreateUser(final Platform platform, final SocialInfoDto socialInfo){ + Optional existedUserOptional = userFinder.isExistedUser(platform, socialInfo.serialId()); + if (existedUserOptional.isEmpty()) { User newUser = User.createUser( socialInfo.name(), socialInfo.email(), @@ -106,22 +109,25 @@ private User loadOrCreateUser(Platform platform, SocialInfoDto socialInfo, boole saveUser(newUser); return newUser; } - return userAdapter.getUser(platform, socialInfo.serialId()); + User existedUser = existedUserOptional.get(); + return updateUserInfo(existedUser); + } + + private User updateUserInfo(final User user) { + user.updateStatus(ACTIVE); + userInfoFinder.getUserInfo(user.getId()).updateNickname(user.getName()); + return user; } private String getRefreshToken(final Long userId) { - return userInfoAdapter.getUserInfo(userId) + return userInfoFinder.getUserInfo(userId) .getRefreshToken(); } private void saveUser(final User user) { - userAdapter.saveUser(user); + userSaver.saveUser(user); UserInfo userInfo = UserInfo.createMemberInfo(user, null); - userInfoAdapter.saveUserInfo(userInfo); - } - - private void updateRefreshToken(final String refreshToken, final UserInfo userInfo) { - userInfo.updateRefreshToken(refreshToken); + userInfoSaver.saveUserInfo(userInfo); } private void validateRefreshToken(final String refreshToken, final long userId) { diff --git a/src/main/java/org/hankki/hankkiserver/api/auth/service/UserAdapter.java b/src/main/java/org/hankki/hankkiserver/api/auth/service/UserFinder.java similarity index 71% rename from src/main/java/org/hankki/hankkiserver/api/auth/service/UserAdapter.java rename to src/main/java/org/hankki/hankkiserver/api/auth/service/UserFinder.java index d3bd5ce6..4cd10ae0 100644 --- a/src/main/java/org/hankki/hankkiserver/api/auth/service/UserAdapter.java +++ b/src/main/java/org/hankki/hankkiserver/api/auth/service/UserFinder.java @@ -9,21 +9,18 @@ import org.hankki.hankkiserver.external.openfeign.dto.SocialInfoDto; import org.springframework.stereotype.Component; +import java.util.Optional; + import static org.hankki.hankkiserver.domain.user.model.MemberStatus.ACTIVE; @Component @RequiredArgsConstructor -public class UserAdapter { +public class UserFinder { private final UserRepository userRepository; - public User getUser(final Platform platform, final String serialId) { - return userRepository.findByPlatformAndSerialIdAndMemberStatus(platform, serialId, ACTIVE) - .orElseThrow(() -> new EntityNotFoundException(ErrorCode.USER_NOT_FOUND)); - } - public User getUser(final long userId) { - return userRepository.findByIdAndMemberStatus(userId, ACTIVE) + return userRepository.findById(userId) .orElseThrow(() -> new EntityNotFoundException(ErrorCode.USER_NOT_FOUND)); } @@ -34,7 +31,7 @@ public boolean isRegisteredUser(final Platform platform, final SocialInfoDto soc ACTIVE); } - public void saveUser(final User user) { - userRepository.save(user); + public Optional isExistedUser(final Platform platform, final String serialId) { + return userRepository.findByPlatformAndSerialId(platform, serialId); } } diff --git a/src/main/java/org/hankki/hankkiserver/api/auth/service/UserInfoDeleter.java b/src/main/java/org/hankki/hankkiserver/api/auth/service/UserInfoDeleter.java new file mode 100644 index 00000000..4b46ef88 --- /dev/null +++ b/src/main/java/org/hankki/hankkiserver/api/auth/service/UserInfoDeleter.java @@ -0,0 +1,16 @@ +package org.hankki.hankkiserver.api.auth.service; + +import lombok.RequiredArgsConstructor; +import org.hankki.hankkiserver.domain.user.repository.UserInfoRepository; +import org.springframework.stereotype.Component; + +@Component +@RequiredArgsConstructor +public class UserInfoDeleter { + + private final UserInfoRepository userInfoRepository; + + public void softDelete(final long userId) { + userInfoRepository.softDeleteByUserId(userId); + } +} diff --git a/src/main/java/org/hankki/hankkiserver/api/auth/service/UserInfoAdapter.java b/src/main/java/org/hankki/hankkiserver/api/auth/service/UserInfoFinder.java similarity index 74% rename from src/main/java/org/hankki/hankkiserver/api/auth/service/UserInfoAdapter.java rename to src/main/java/org/hankki/hankkiserver/api/auth/service/UserInfoFinder.java index be82edfe..b34a1dd7 100644 --- a/src/main/java/org/hankki/hankkiserver/api/auth/service/UserInfoAdapter.java +++ b/src/main/java/org/hankki/hankkiserver/api/auth/service/UserInfoFinder.java @@ -9,7 +9,7 @@ @Component @RequiredArgsConstructor -public class UserInfoAdapter { +public class UserInfoFinder { private final UserInfoRepository userInfoRepository; @@ -17,13 +17,4 @@ public UserInfo getUserInfo(final long userId) { return userInfoRepository.findByUserId(userId) .orElseThrow(() -> new EntityNotFoundException(ErrorCode.USER_INFO_NOT_FOUND)); } - - public void saveUserInfo(final UserInfo userInfo) { - userInfoRepository.save(userInfo); - } - - public void softDelete(final long userId) { - userInfoRepository.softDeleteByUserId(userId); - } - } diff --git a/src/main/java/org/hankki/hankkiserver/api/auth/service/UserInfoSaver.java b/src/main/java/org/hankki/hankkiserver/api/auth/service/UserInfoSaver.java new file mode 100644 index 00000000..dc0f0e77 --- /dev/null +++ b/src/main/java/org/hankki/hankkiserver/api/auth/service/UserInfoSaver.java @@ -0,0 +1,17 @@ +package org.hankki.hankkiserver.api.auth.service; + +import lombok.RequiredArgsConstructor; +import org.hankki.hankkiserver.domain.user.model.UserInfo; +import org.hankki.hankkiserver.domain.user.repository.UserInfoRepository; +import org.springframework.stereotype.Component; + +@Component +@RequiredArgsConstructor +public class UserInfoSaver { + + private final UserInfoRepository userInfoRepository; + + public void saveUserInfo(final UserInfo userInfo) { + userInfoRepository.save(userInfo); + } +} diff --git a/src/main/java/org/hankki/hankkiserver/api/auth/service/UserSaver.java b/src/main/java/org/hankki/hankkiserver/api/auth/service/UserSaver.java new file mode 100644 index 00000000..e6a51480 --- /dev/null +++ b/src/main/java/org/hankki/hankkiserver/api/auth/service/UserSaver.java @@ -0,0 +1,17 @@ +package org.hankki.hankkiserver.api.auth.service; + +import lombok.RequiredArgsConstructor; +import org.hankki.hankkiserver.domain.user.model.User; +import org.hankki.hankkiserver.domain.user.repository.UserRepository; +import org.springframework.stereotype.Component; + +@Component +@RequiredArgsConstructor +public class UserSaver { + + private final UserRepository userRepository; + + public void saveUser(final User user) { + userRepository.save(user); + } +} diff --git a/src/main/java/org/hankki/hankkiserver/domain/user/model/Platform.java b/src/main/java/org/hankki/hankkiserver/domain/user/model/Platform.java index f0de90cf..5ea84b49 100644 --- a/src/main/java/org/hankki/hankkiserver/domain/user/model/Platform.java +++ b/src/main/java/org/hankki/hankkiserver/domain/user/model/Platform.java @@ -2,6 +2,10 @@ import lombok.Getter; import lombok.RequiredArgsConstructor; +import org.hankki.hankkiserver.common.code.ErrorCode; +import org.hankki.hankkiserver.common.exception.InvalidValueException; + +import java.util.Arrays; @Getter @RequiredArgsConstructor @@ -11,4 +15,11 @@ public enum Platform { APPLE("APPLE"); private final String loginPlatform; + + public static Platform getEnumPlatformFromStringPlatform(String loginPlatform) { + return Arrays.stream(values()) + .filter(platform -> platform.loginPlatform.equals(loginPlatform)) + .findFirst() + .orElseThrow(() -> new InvalidValueException(ErrorCode.INVALID_PLATFORM_TYPE)); + } } diff --git a/src/main/java/org/hankki/hankkiserver/domain/user/model/User.java b/src/main/java/org/hankki/hankkiserver/domain/user/model/User.java index 9eab3920..6bd76ae8 100644 --- a/src/main/java/org/hankki/hankkiserver/domain/user/model/User.java +++ b/src/main/java/org/hankki/hankkiserver/domain/user/model/User.java @@ -55,8 +55,8 @@ public static User createUser(final String name, final String email, final Strin .build(); } - public void softDelete(MemberStatus memberStatus) { - updateStatus(memberStatus); + public void softDelete() { + updateStatus(INACTIVE); this.deletedAt = LocalDateTime.now(); } diff --git a/src/main/java/org/hankki/hankkiserver/domain/user/repository/UserRepository.java b/src/main/java/org/hankki/hankkiserver/domain/user/repository/UserRepository.java index 5b8dffc1..31f1f77d 100644 --- a/src/main/java/org/hankki/hankkiserver/domain/user/repository/UserRepository.java +++ b/src/main/java/org/hankki/hankkiserver/domain/user/repository/UserRepository.java @@ -8,8 +8,8 @@ import java.util.Optional; public interface UserRepository extends JpaRepository { + Optional findByPlatformAndSerialId(Platform platform, String serialId); boolean existsByPlatformAndSerialIdAndMemberStatus(Platform platform, String serialId, MemberStatus memberStatus); Optional findByPlatformAndSerialIdAndMemberStatus(Platform platform, String serialId, MemberStatus memberStatus); - Optional findByIdAndMemberStatus(Long Userid, MemberStatus memberStatus); } diff --git a/src/main/java/org/hankki/hankkiserver/external/openfeign/apple/AppleFeignClient.java b/src/main/java/org/hankki/hankkiserver/external/openfeign/apple/AppleFeignClient.java index 42b392e4..6a30a224 100644 --- a/src/main/java/org/hankki/hankkiserver/external/openfeign/apple/AppleFeignClient.java +++ b/src/main/java/org/hankki/hankkiserver/external/openfeign/apple/AppleFeignClient.java @@ -8,7 +8,7 @@ import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PostMapping; -@FeignClient(name = "${oauth.apple.name}", url = "${oauth.apple.url}") +@FeignClient(name = "appleClient", url = "https://appleid.apple.com/auth") public interface AppleFeignClient { @GetMapping("/keys") diff --git a/src/main/java/org/hankki/hankkiserver/external/openfeign/kakao/KakaoFeignClient.java b/src/main/java/org/hankki/hankkiserver/external/openfeign/kakao/KakaoFeignClient.java index 4d23897f..3b53f80a 100644 --- a/src/main/java/org/hankki/hankkiserver/external/openfeign/kakao/KakaoFeignClient.java +++ b/src/main/java/org/hankki/hankkiserver/external/openfeign/kakao/KakaoFeignClient.java @@ -6,7 +6,7 @@ import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestHeader; -@FeignClient(name = "${oauth.kakao.name}", url = "${oauth.kakao.url}") +@FeignClient(name = "kakaoClient", url = "https://kapi.kakao.com/v2/user/me") public interface KakaoFeignClient { @GetMapping