Skip to content

Commit

Permalink
Merge pull request #112 from Happy-HOBAK/feat/#111
Browse files Browse the repository at this point in the history
[#111] ๋กœ๊ทธ์ธ ์‘๋‹ต์— ์œ ์ € ์ด๋ฆ„๋„ ํฌํ•จํ•˜๋„๋ก ์ˆ˜์ •
  • Loading branch information
KkomSang authored May 30, 2024
2 parents 69a3a01 + 6186522 commit b2792ab
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -33,17 +33,17 @@ public class UserController {
@NonNull
private PasswordEncoder passwordEncoder;
@GetMapping("/profile")
public DataResponseDto<Object> getUserInfo(@AuthenticationPrincipal UserDetails userDetails) {
public DataResponseDto<Object> getUserProfile(@AuthenticationPrincipal UserDetails userDetails) {
User user = userFindService.findByUserDetails(userDetails);
UserInfoResponseDto userInfoResponseDto = UserConverter.toUserInfoResponseDto(user);
return DataResponseDto.of(userInfoResponseDto, "์œ ์ € ํ”„๋กœํ•„์„ ์„ฑ๊ณต์ ์œผ๋กœ ์กฐํšŒํ–ˆ์Šต๋‹ˆ๋‹ค.");
UserProfileResponseDto userProfileResponseDto = UserConverter.toUserProfileResponseDto(user);
return DataResponseDto.of(userProfileResponseDto, "์œ ์ € ํ”„๋กœํ•„์„ ์„ฑ๊ณต์ ์œผ๋กœ ์กฐํšŒํ–ˆ์Šต๋‹ˆ๋‹ค.");
}

@PutMapping("/profile")
public DataResponseDto<Object> updateUserInfo(@RequestBody @Valid UserProfileUpdateRequestDto requestDto, @AuthenticationPrincipal UserDetails userDetails) {
public DataResponseDto<Object> updateUserProfile(@RequestBody @Valid UserProfileUpdateRequestDto requestDto, @AuthenticationPrincipal UserDetails userDetails) {
User user = userFindService.findByUserDetails(userDetails);
User updatedUser = userProfileService.updateUserProfile(user, requestDto);
UserInfoResponseDto responseDto = UserConverter.toUserInfoResponseDto(updatedUser);
UserProfileResponseDto responseDto = UserConverter.toUserProfileResponseDto(updatedUser);

return DataResponseDto.of(responseDto, "์œ ์ € ํ”„๋กœํ•„์„ ์„ฑ๊ณต์ ์œผ๋กœ ์ˆ˜์ •ํ–ˆ์Šต๋‹ˆ๋‹ค.");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@
import com.hobak.happinessql.domain.user.domain.User;
import com.hobak.happinessql.domain.user.dto.SignUpDto;
import com.hobak.happinessql.domain.user.dto.UserDto;
import com.hobak.happinessql.domain.user.dto.UserInfoResponseDto;
import com.hobak.happinessql.domain.user.dto.UserProfileResponseDto;
import org.springframework.security.core.userdetails.UserDetails;
import java.util.List;

public class UserConverter {
public static UserInfoResponseDto toUserInfoResponseDto(User user) {
return UserInfoResponseDto.builder()
public static UserProfileResponseDto toUserProfileResponseDto(User user) {
return UserProfileResponseDto.builder()
.userId(user.getUserId())
.name(user.getName())
.age(user.getAge())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

@Getter
@NoArgsConstructor(access = AccessLevel.PROTECTED)
public class UserInfoResponseDto {
public class UserProfileResponseDto {

private Long userId;

Expand All @@ -20,7 +20,7 @@ public class UserInfoResponseDto {
private int age;

@Builder
public UserInfoResponseDto(Long userId, String name, Gender gender, int age) {
public UserProfileResponseDto(Long userId, String name, Gender gender, int age) {
this.userId = userId;
this.name = name;
this.gender = gender;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,5 @@ public class JwtToken {
private String grantType;
private String accessToken;
private String refreshToken;
private String name;
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.stereotype.Component;
import java.security.Key;
Expand All @@ -20,10 +21,12 @@
public class JwtTokenProvider {
private final Key key;
private final CustomUserDetailsService customUserDetailsService;
public JwtTokenProvider(@Value("${jwt.secret}")String secretKey, CustomUserDetailsService customUserDetailsService){
private final UserFindService userFindService;
public JwtTokenProvider(@Value("${jwt.secret}")String secretKey, CustomUserDetailsService customUserDetailsService, UserFindService userFindService){
byte[] keyBytes = Decoders.BASE64.decode(secretKey);
this.key = Keys.hmacShaKeyFor(keyBytes);
this.customUserDetailsService = customUserDetailsService;
this.userFindService = userFindService;
}
// Member ์ •๋ณด๋ฅผ ๊ฐ€์ง€๊ณ  AccessToken, RefreshToken์„ ์ƒ์„ฑํ•˜๋Š” ๋ฉ”์„œ๋“œ
public JwtToken generateToken(Authentication authentication) {
Expand All @@ -49,10 +52,15 @@ public JwtToken generateToken(Authentication authentication) {
.signWith(key, SignatureAlgorithm.HS256)
.compact();

// CustomUserDetails ๊ฐ์ฒด์—์„œ name ๊ฐ€์ ธ์˜ค๊ธฐ
User userDetails = (User) authentication.getPrincipal();
String name = userFindService.findByUserDetails(userDetails).getName();

return JwtToken.builder()
.grantType("Bearer")
.accessToken(accessToken)
.refreshToken(refreshToken)
.name(name)
.build();
}

Expand Down

0 comments on commit b2792ab

Please sign in to comment.