From 52499bc0f5768f17a4d98986d7b21d391cecadbb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E1=84=8E=E1=85=AC=E1=84=8C=E1=85=AE=E1=86=AB=E1=84=92?= =?UTF-8?q?=E1=85=A9?= Date: Wed, 10 Jan 2024 19:35:03 +0900 Subject: [PATCH] =?UTF-8?q?refactor:=20=EC=82=AC=EC=9A=A9=EC=9E=90=20?= =?UTF-8?q?=EC=9D=B8=EC=A6=9D=20=EC=9A=94=EA=B5=AC=EC=82=AC=ED=95=AD=20?= =?UTF-8?q?=EC=A0=9C=EA=B1=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../koin/domain/user/controller/UserController.java | 3 +-- .../koin/domain/user/repository/UserTokenRepository.java | 2 ++ .../in/koreatech/koin/domain/user/service/UserService.java | 6 ++++-- 3 files changed, 7 insertions(+), 4 deletions(-) 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()); }