Skip to content

Commit 20b3512

Browse files
authored
Merge pull request #314 from GTable/feature/#310-phoneNumber
refactor: 토큰 재발급 로직 수정
2 parents 1daae21 + ed039b6 commit 20b3512

4 files changed

Lines changed: 27 additions & 26 deletions

File tree

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package com.nowait.applicationuser.token.dto;
2+
3+
import com.fasterxml.jackson.annotation.JsonProperty;
4+
5+
import lombok.Getter;
6+
import lombok.RequiredArgsConstructor;
7+
import lombok.ToString;
8+
9+
@RequiredArgsConstructor
10+
@Getter
11+
@ToString(exclude = {"accessToken"}) // 로깅 시 토큰 노출 방지
12+
public class NewAccessTokenResponse {
13+
@JsonProperty("access_token")
14+
private final String accessToken;
15+
}

nowait-app-user-api/src/main/java/com/nowait/applicationuser/user/controller/UserController.java

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,17 @@
22

33
import org.springframework.http.HttpStatus;
44
import org.springframework.http.ResponseEntity;
5-
import org.springframework.security.core.annotation.AuthenticationPrincipal;
65
import org.springframework.web.bind.annotation.CookieValue;
76
import org.springframework.web.bind.annotation.PutMapping;
87
import org.springframework.web.bind.annotation.RequestBody;
98
import org.springframework.web.bind.annotation.RequestMapping;
109
import org.springframework.web.bind.annotation.RestController;
1110

1211
import com.nowait.applicationuser.token.dto.AuthenticationResponse;
12+
import com.nowait.applicationuser.token.dto.NewAccessTokenResponse;
1313
import com.nowait.applicationuser.user.dto.UserUpdateRequest;
1414
import com.nowait.applicationuser.user.service.UserService;
1515
import com.nowait.common.api.ApiUtils;
16-
import com.nowait.domainuserrdb.oauth.dto.CustomOAuth2User;
1716

1817
import jakarta.validation.Valid;
1918
import lombok.RequiredArgsConstructor;
@@ -27,21 +26,16 @@ public class UserController {
2726

2827
@PutMapping("/optional-info")
2928
public ResponseEntity<?> putOptional(
30-
@CookieValue(value = "refreshToken", required = false) String refreshToken,
3129
@Valid @RequestBody UserUpdateRequest req) {
3230

33-
if (refreshToken == null) {
34-
return ResponseEntity.status(HttpStatus.UNAUTHORIZED).body("accessToken not found in cookies");
35-
}
36-
37-
AuthenticationResponse authenticationResponse = userService.putOptional(refreshToken, req.phoneNumber(),
38-
Boolean.TRUE.equals(req.consent()));
31+
NewAccessTokenResponse newAccessTokenResponse = userService.putOptional(req.phoneNumber(),
32+
Boolean.TRUE.equals(req.consent()), req.accessToken());
3933

4034
return ResponseEntity
4135
.status(HttpStatus.OK)
4236
.body(
4337
ApiUtils.success(
44-
authenticationResponse
38+
newAccessTokenResponse
4539
)
4640
);
4741
}

nowait-app-user-api/src/main/java/com/nowait/applicationuser/user/dto/UserUpdateRequest.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,5 @@ public record UserUpdateRequest(
77
@NotBlank
88
@Pattern(regexp = "^010-\\d{4}-\\d{4}$", message = "휴대폰 번호는 010-0000-0000 형식이어야 합니다.")
99
String phoneNumber,
10-
boolean consent) { }
10+
boolean consent,
11+
String accessToken) { }

nowait-app-user-api/src/main/java/com/nowait/applicationuser/user/service/UserService.java

Lines changed: 6 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,12 @@
22

33
import java.time.LocalDateTime;
44

5-
import org.springframework.http.HttpStatus;
6-
import org.springframework.http.ResponseEntity;
7-
import org.springframework.security.core.Authentication;
85
import org.springframework.stereotype.Service;
96
import org.springframework.transaction.annotation.Transactional;
107

118
import com.nowait.applicationuser.security.jwt.JwtUtil;
129
import com.nowait.applicationuser.token.dto.AuthenticationResponse;
10+
import com.nowait.applicationuser.token.dto.NewAccessTokenResponse;
1311
import com.nowait.applicationuser.token.service.TokenService;
1412
import com.nowait.domaincorerdb.user.entity.User;
1513
import com.nowait.domaincorerdb.user.exception.UserNotFoundException;
@@ -26,10 +24,10 @@ public class UserService {
2624
private final JwtUtil jwtUtil;
2725

2826
@Transactional
29-
public AuthenticationResponse putOptional(String refreshToken, String phoneNumber, boolean consent) {
27+
public NewAccessTokenResponse putOptional(String phoneNumber, boolean consent, String accessToken) {
3028

31-
Long userId = jwtUtil.getUserId(refreshToken);;
32-
String role = jwtUtil.getRole(refreshToken);
29+
Long userId = jwtUtil.getUserId(accessToken);;
30+
String role = jwtUtil.getRole(accessToken);
3331
AuthenticationResponse authenticationResponse;
3432

3533
User user = userRepository.findById(userId).orElseThrow(UserNotFoundException::new);
@@ -49,16 +47,9 @@ public AuthenticationResponse putOptional(String refreshToken, String phoneNumbe
4947
Boolean.TRUE.equals(user.getIsMarketingAgree()),
5048
60 * 60 * 1000L
5149
);
52-
String newRefreshToken = jwtUtil.createRefreshToken(
53-
"refreshToken",
54-
userId,
55-
60 * 60 * 1000L
56-
);
57-
58-
tokenService.updateRefreshToken(userId, refreshToken, newRefreshToken);
5950

60-
authenticationResponse = new AuthenticationResponse(newAccessToken, newRefreshToken);
51+
NewAccessTokenResponse newAccessTokenResponse = new NewAccessTokenResponse(newAccessToken);
6152

62-
return authenticationResponse;
53+
return newAccessTokenResponse;
6354
}
6455
}

0 commit comments

Comments
 (0)