Skip to content

Commit

Permalink
[refac] apply code review
Browse files Browse the repository at this point in the history
  • Loading branch information
kgy1008 committed Jul 8, 2024
1 parent 674675c commit 22b6dd7
Show file tree
Hide file tree
Showing 13 changed files with 112 additions and 58 deletions.
2 changes: 1 addition & 1 deletion server-yml
Original file line number Diff line number Diff line change
@@ -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
) {
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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;
Expand All @@ -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);
Expand All @@ -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;
}

Expand All @@ -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<User> existedUserOptional = userFinder.isExistedUser(platform, socialInfo.serialId());
if (existedUserOptional.isEmpty()) {
User newUser = User.createUser(
socialInfo.name(),
socialInfo.email(),
Expand All @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}

Expand All @@ -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<User> isExistedUser(final Platform platform, final String serialId) {
return userRepository.findByPlatformAndSerialId(platform, serialId);
}
}
Original file line number Diff line number Diff line change
@@ -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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,21 +9,12 @@

@Component
@RequiredArgsConstructor
public class UserInfoAdapter {
public class UserInfoFinder {

private final UserInfoRepository userInfoRepository;

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);
}

}
Original file line number Diff line number Diff line change
@@ -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);
}
}
Original file line number Diff line number Diff line change
@@ -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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
import java.util.Optional;

public interface UserRepository extends JpaRepository<User, Long> {
Optional<User> findByPlatformAndSerialId(Platform platform, String serialId);

boolean existsByPlatformAndSerialIdAndMemberStatus(Platform platform, String serialId, MemberStatus memberStatus);
Optional<User> findByPlatformAndSerialIdAndMemberStatus(Platform platform, String serialId, MemberStatus memberStatus);
Optional<User> findByIdAndMemberStatus(Long Userid, MemberStatus memberStatus);
}
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 22b6dd7

Please sign in to comment.