Skip to content

Commit

Permalink
refac: UserService에서 사용자 관련 로직만 처리하도록 수정
Browse files Browse the repository at this point in the history
  • Loading branch information
Jeongmin39 committed Jan 3, 2025
1 parent 2632124 commit 2a59792
Showing 1 changed file with 11 additions and 37 deletions.
48 changes: 11 additions & 37 deletions src/main/java/com/spot/spotserver/api/user/service/UserService.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,6 @@

import com.spot.spotserver.api.auth.client.KakaoAccount;
import com.spot.spotserver.api.auth.dto.response.KakaoUserResponse;
import com.spot.spotserver.api.auth.dto.response.TokenResponse;
import com.spot.spotserver.api.auth.exception.JwtCustomException;
import com.spot.spotserver.api.auth.handler.UserAuthentication;
import com.spot.spotserver.api.auth.jwt.JwtTokenProvider;
import com.spot.spotserver.api.auth.jwt.JwtValidationType;
import com.spot.spotserver.api.auth.jwt.redis.RefreshTokenService;
import com.spot.spotserver.api.badge.domain.Badge;
import com.spot.spotserver.api.quiz.dto.UserBadgeResponse;
import com.spot.spotserver.api.badge.repository.BadgeRepository;
Expand Down Expand Up @@ -37,13 +31,19 @@
@RequiredArgsConstructor
public class UserService {

private final JwtTokenProvider jwtTokenProvider;
private final UserRepository userRepository;
private final LikesRepository likesRepository;
private final BadgeRepository badgeRepository;
private final RefreshTokenService refreshTokenService;
private final S3Service s3Service;

public Long processUser(final KakaoUserResponse userResponse) {
if (isExistingUser(userResponse.id())) {
return getIdBySocialId(userResponse.id());
} else {
return createUser(userResponse);
}
}

public Long createUser(final KakaoUserResponse userResponse) {
String email = Optional.ofNullable(userResponse.kakaoAccount())
.map(KakaoAccount::email)
Expand All @@ -53,41 +53,15 @@ public Long createUser(final KakaoUserResponse userResponse) {
}

public Long getIdBySocialId(final Long socialId) {
User user = userRepository.findBySocialId(socialId)
.orElseThrow(() -> new UserNotFoundException(ErrorCode.USER_NOT_FOUND));
return user.getId();
return userRepository.findBySocialId(socialId)
.orElseThrow(() -> new UserNotFoundException(ErrorCode.USER_NOT_FOUND))
.getId();
}

public boolean isExistingUser(final Long socialId) {
return userRepository.findBySocialId(socialId).isPresent();
}

public TokenResponse getTokenByUserId(final Long id) {
UserAuthentication userAuthentication = new UserAuthentication(id, null, null);
String refreshToken = jwtTokenProvider.issueRefreshToken(userAuthentication);
refreshTokenService.saveRefreshToken(id, refreshToken);
return TokenResponse.of(
jwtTokenProvider.issueAccessToken(userAuthentication),
refreshToken
);
}

public TokenResponse reissueToken(final String refreshToken) {
JwtValidationType validationType = jwtTokenProvider.validateToken(refreshToken);
if (validationType != JwtValidationType.VALID_JWT) {
throw new JwtCustomException(ErrorCode.INVALID_JWT_TOKEN);
}

Long userId = jwtTokenProvider.getUserFromJwt(refreshToken);
UserAuthentication userAuthentication = new UserAuthentication(userId, null, null);
String newAccessToken = jwtTokenProvider.issueAccessToken(userAuthentication);
String newRefreshToken = jwtTokenProvider.issueRefreshToken(userAuthentication);

// 새로운 리프레시 토큰으로 교체
refreshTokenService.saveRefreshToken(userId, newRefreshToken);
return TokenResponse.of(newAccessToken, newRefreshToken);
}

public String saveNickname(NicknameRequest nicknameRequest, User user) {
userRepository.findById(user.getId())
.orElseThrow(() -> new UserNotFoundException(ErrorCode.USER_NOT_FOUND));
Expand Down

0 comments on commit 2a59792

Please sign in to comment.