diff --git a/src/main/java/ice/spot/controller/AuthController.java b/src/main/java/ice/spot/controller/AuthController.java index 173b2a9..49153ae 100644 --- a/src/main/java/ice/spot/controller/AuthController.java +++ b/src/main/java/ice/spot/controller/AuthController.java @@ -1,11 +1,20 @@ package ice.spot.controller; import ice.spot.annotation.UserId; +import ice.spot.constant.Constants; import ice.spot.dto.global.ResponseDto; import ice.spot.dto.request.OauthSignUpDto; +import ice.spot.dto.response.JwtTokenDto; +import ice.spot.exception.CommonException; +import ice.spot.exception.ErrorCode; import ice.spot.service.AuthService; +import ice.spot.util.CookieUtil; +import ice.spot.util.HeaderUtil; +import jakarta.servlet.http.HttpServletRequest; +import jakarta.servlet.http.HttpServletResponse; import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; +import org.springframework.beans.factory.annotation.Value; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestMapping; @@ -17,6 +26,9 @@ @RequiredArgsConstructor public class AuthController { + @Value("${server.domain}") + private String domain; + private final AuthService authService; @PostMapping("/oauth2/sign-up") @@ -24,4 +36,21 @@ public ResponseDto signUp(@UserId Long userId, @RequestBody OauthSignUpDto oa authService.signUp(userId, oauthSignUpDto); return ResponseDto.ok(null); } + + @PostMapping("/auth/reissue") + public ResponseDto reissue( + HttpServletRequest request, + HttpServletResponse response, + @UserId Long userId){ + log.info("controller 진입 성공"); + String refreshToken = HeaderUtil.refineHeader(request, Constants.PREFIX_AUTH, Constants.PREFIX_BEARER) + .orElseThrow(() -> new CommonException(ErrorCode.INVALID_HEADER_VALUE)); + log.info("헤더값 조회 성공"); + JwtTokenDto jwtTokenDto = authService.reGenerateTokens(userId, refreshToken); + + CookieUtil.addCookie(response, domain, Constants.ACCESS_COOKIE_NAME, jwtTokenDto.accessToken()); + CookieUtil.addSecureCookie(response, domain, Constants.REFRESH_COOKIE_NAME, jwtTokenDto.refreshToken(), 60 * 60 * 24 * 14); + + return ResponseDto.ok(jwtTokenDto); + } } diff --git a/src/main/java/ice/spot/service/AuthService.java b/src/main/java/ice/spot/service/AuthService.java index e4a89bc..4b7d553 100644 --- a/src/main/java/ice/spot/service/AuthService.java +++ b/src/main/java/ice/spot/service/AuthService.java @@ -2,9 +2,11 @@ import ice.spot.domain.User; import ice.spot.dto.request.OauthSignUpDto; +import ice.spot.dto.response.JwtTokenDto; import ice.spot.exception.CommonException; import ice.spot.exception.ErrorCode; import ice.spot.repository.UserRepository; +import ice.spot.util.JwtUtil; import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; import org.springframework.stereotype.Service; @@ -16,6 +18,7 @@ public class AuthService { private final UserRepository userRepository; + private final JwtUtil jwtUtil; @Transactional public void signUp(Long userId, OauthSignUpDto oauthSignUpDto){ @@ -25,4 +28,15 @@ public void signUp(Long userId, OauthSignUpDto oauthSignUpDto){ oauthUser.register(oauthSignUpDto.nickname()); } + @Transactional + public JwtTokenDto reGenerateTokens(Long userId, String refreshToken){ + log.info("re generate tokens 진입성공"); + User loginUser = userRepository.findByIdAndRefreshToken(userId, refreshToken) + .orElseThrow(() -> new CommonException(ErrorCode.NOT_FOUND_USER)); + log.info("유저 조회 성공"); + JwtTokenDto jwtTokenDto = jwtUtil.generateTokens(loginUser.getId(), loginUser.getRole()); + + loginUser.updateRefreshToken(jwtTokenDto.refreshToken()); + return jwtTokenDto; + } }