diff --git a/src/main/java/in/koreatech/koin/domain/user/controller/UserController.java b/src/main/java/in/koreatech/koin/domain/user/controller/UserController.java index 7df7b7728..073efcd0d 100644 --- a/src/main/java/in/koreatech/koin/domain/user/controller/UserController.java +++ b/src/main/java/in/koreatech/koin/domain/user/controller/UserController.java @@ -36,10 +36,9 @@ public ResponseEntity logout(@UserAuth User user) { @PostMapping("/user/refresh") public ResponseEntity refresh( - @UserAuth User user, @RequestBody @Valid UserTokenRefreshRequest request ) { - UserTokenRefreshResponse tokenGroupResponse = userService.refresh(user, request); + UserTokenRefreshResponse tokenGroupResponse = userService.refresh(request); return ResponseEntity.ok().body(tokenGroupResponse); } } diff --git a/src/main/java/in/koreatech/koin/domain/user/repository/UserTokenRepository.java b/src/main/java/in/koreatech/koin/domain/user/repository/UserTokenRepository.java index 1af8fdd11..0a3d15984 100644 --- a/src/main/java/in/koreatech/koin/domain/user/repository/UserTokenRepository.java +++ b/src/main/java/in/koreatech/koin/domain/user/repository/UserTokenRepository.java @@ -10,5 +10,7 @@ public interface UserTokenRepository extends Repository { Optional findById(Long userId); + Optional findByRefreshToken(String refreshToken); + void deleteById(Long id); } diff --git a/src/main/java/in/koreatech/koin/domain/user/service/UserService.java b/src/main/java/in/koreatech/koin/domain/user/service/UserService.java index f86a148a7..be5dda27c 100644 --- a/src/main/java/in/koreatech/koin/domain/user/service/UserService.java +++ b/src/main/java/in/koreatech/koin/domain/user/service/UserService.java @@ -49,12 +49,14 @@ public void logout(User user) { userTokenRepository.deleteById(user.getId()); } - public UserTokenRefreshResponse refresh(User user, UserTokenRefreshRequest request) { - UserToken userToken = userTokenRepository.findById(user.getId()) + public UserTokenRefreshResponse refresh(UserTokenRefreshRequest request) { + UserToken userToken = userTokenRepository.findByRefreshToken(request.refreshToken()) .orElseThrow(() -> new IllegalArgumentException("refresh token이 존재하지 않습니다. request: " + request)); if (!Objects.equals(userToken.getRefreshToken(), request.refreshToken())) { throw new IllegalArgumentException("refresh token이 일치하지 않습니다. request: " + request); } + User user = userRepository.findById(userToken.getId()) + .orElseThrow(() -> new UserNotFoundException("사용자를 찾을 수 없습니다. refreshToken: " + userToken)); String accessToken = jwtProvider.createToken(user); return UserTokenRefreshResponse.of(accessToken, userToken.getRefreshToken()); }