diff --git a/week6/build/classes/java/main/org/sopt/spring/service/MemberService.class b/week6/build/classes/java/main/org/sopt/spring/service/MemberService.class new file mode 100644 index 0000000..6a20b19 Binary files /dev/null and b/week6/build/classes/java/main/org/sopt/spring/service/MemberService.class differ diff --git a/week6/src/main/java/org/sopt/spring/auth/redis/Service/TokenService.java b/week6/src/main/java/org/sopt/spring/auth/redis/Service/TokenService.java index 457800d..be6b4d2 100644 --- a/week6/src/main/java/org/sopt/spring/auth/redis/Service/TokenService.java +++ b/week6/src/main/java/org/sopt/spring/auth/redis/Service/TokenService.java @@ -31,6 +31,15 @@ public void saveRefreshToken( ); } + @Transactional + public String generateRefreshToken(Long userId) { + String refreshToken = jwtTokenProvider.issueRefreshToken( + UserAuthentication.createUserAuthentication(userId) + ); + saveRefreshToken(userId, refreshToken); + return refreshToken; + } + @Transactional public void deleteRefreshToken( final Long userId @@ -51,15 +60,10 @@ public Long findIdByRefreshToken( return token.getId(); } - protected String generateAccessToken(Long userId) { + public String generateAccessToken(Long userId) { return jwtTokenProvider.issueAccessToken( UserAuthentication.createUserAuthentication(userId) ); } - protected String generateRefreshToken(Long userId) { - return jwtTokenProvider.issueRefreshToken( - UserAuthentication.createUserAuthentication(userId) - ); - } } \ No newline at end of file diff --git a/week6/src/main/java/org/sopt/spring/controller/MemberController.java b/week6/src/main/java/org/sopt/spring/controller/MemberController.java index 3a8cd0c..cc0b0fe 100644 --- a/week6/src/main/java/org/sopt/spring/controller/MemberController.java +++ b/week6/src/main/java/org/sopt/spring/controller/MemberController.java @@ -2,6 +2,8 @@ import lombok.RequiredArgsConstructor; +import org.sopt.spring.auth.redis.Service.TokenService; +import org.sopt.spring.exception.NotFoundException; import org.sopt.spring.service.MemberService; import org.sopt.spring.service.dto.MemberCreateDto; import org.sopt.spring.service.dto.MemberFindDto; @@ -26,6 +28,7 @@ public class MemberController { private final MemberService memberService; + private final TokenService tokenService; @PostMapping public ResponseEntity postMember( @@ -39,6 +42,15 @@ public ResponseEntity postMember( ); } + @PostMapping("/members/refresh") + public ResponseEntity login( + @RequestHeader(name = "refreshToken") String refreshToken + ) { + Long userId = tokenService.findIdByRefreshToken(refreshToken); // Refresh Token으로부터 userId 추출 + String accessToken = tokenService.generateAccessToken(userId); // 새로운 Access Token 발급 + return ResponseEntity.ok(new UserJoinResponse(accessToken, refreshToken, userId.toString())); + } + @GetMapping("/{memberId}") public ResponseEntity findMemberById(@PathVariable Long memberId) { return ResponseEntity.ok(memberService.findMemberById(memberId)); diff --git a/week6/src/main/java/org/sopt/spring/service/dto/TokenResponse.java b/week6/src/main/java/org/sopt/spring/service/dto/TokenResponse.java new file mode 100644 index 0000000..9dcc881 --- /dev/null +++ b/week6/src/main/java/org/sopt/spring/service/dto/TokenResponse.java @@ -0,0 +1,9 @@ +package org.sopt.spring.service.dto; + +public record TokenResponse ( + String accessToken +) { + public static TokenResponse of(final String accessToken) { + return new TokenResponse(accessToken); + } +}