Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

✨ [Feature] - Reissue 토큰 기능 추가 #31

Merged
merged 3 commits into from
May 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion spot-server-properties
29 changes: 29 additions & 0 deletions src/main/java/ice/spot/controller/AuthController.java
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -17,11 +26,31 @@
@RequiredArgsConstructor
public class AuthController {

@Value("${server.domain}")
private String domain;

private final AuthService authService;

@PostMapping("/oauth2/sign-up")
public ResponseDto<?> signUp(@UserId Long userId, @RequestBody OauthSignUpDto oauthSignUpDto) {
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);
}
}
14 changes: 14 additions & 0 deletions src/main/java/ice/spot/service/AuthService.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -16,6 +18,7 @@
public class AuthService {

private final UserRepository userRepository;
private final JwtUtil jwtUtil;

@Transactional
public void signUp(Long userId, OauthSignUpDto oauthSignUpDto){
Expand All @@ -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;
}
}