diff --git a/src/main/java/com/ticle/server/user/controller/UserController.java b/src/main/java/com/ticle/server/user/controller/UserController.java index 90123ca..6cb7c8b 100644 --- a/src/main/java/com/ticle/server/user/controller/UserController.java +++ b/src/main/java/com/ticle/server/user/controller/UserController.java @@ -3,9 +3,8 @@ import com.ticle.server.global.dto.ResponseTemplate; import com.ticle.server.user.dto.request.*; import com.ticle.server.user.dto.response.JwtTokenResponse; +import com.ticle.server.user.dto.response.UserInfoResponse; import com.ticle.server.user.dto.response.UserResponse; -import com.ticle.server.user.jwt.JwtTokenProvider; -import com.ticle.server.user.repository.UserRepository; import com.ticle.server.user.jwt.CustomUserDetails; import com.ticle.server.user.service.UserService; import io.swagger.v3.oas.annotations.Operation; @@ -28,8 +27,6 @@ public class UserController { private final UserService userService; - private final UserRepository userRepository; - private final JwtTokenProvider jwtTokenProvider; @Operation(summary = "로그인", description = "email과 password로 로그인을 진행합니다.") @PostMapping("/sign-in") @@ -50,16 +47,6 @@ public ResponseEntity> signUp(@RequestBody JoinRequest .body(ResponseTemplate.from(savedUserDto)); } -// @Operation(summary = "관심직무 선택", description = "회원가입 이후 ") -// @PostMapping("/sign-up/category") -// public ResponseEntity> selectCategory(@RequestBody CategoryRequest categoryRequest){ -// userService.addCategory(categoryRequest); -// -// return ResponseEntity -// .status(OK) -// .body(ResponseTemplate.from(categoryRequest.toString())); -// } - @Operation(summary = "로그아웃", description = "user의 email을 받아와서 redis에서 email을 삭제, accessToken을 블랙리스트 처리합니다. ") @DeleteMapping("/logout") public ResponseEntity> logout(@AuthenticationPrincipal CustomUserDetails userDetails, HttpServletRequest request){ @@ -111,4 +98,15 @@ public ResponseEntity> deleteUser(@AuthenticationPrinci .body(ResponseTemplate.from(customUserDetails.getUserId() + "님의 회원정보가 삭제되었습니다.\n" )); } + @Operation(summary = "유저 정보 조회", description = "유저의 정보를 조회") + @GetMapping("/profile") + public ResponseEntity> getUserInfo( + @AuthenticationPrincipal CustomUserDetails userDetails){ + + UserInfoResponse response = userService.getUserInfo(userDetails); + + return ResponseEntity + .status(OK) + .body(ResponseTemplate.from(response)); + } } diff --git a/src/main/java/com/ticle/server/user/dto/response/UserInfoResponse.java b/src/main/java/com/ticle/server/user/dto/response/UserInfoResponse.java new file mode 100644 index 0000000..368a17a --- /dev/null +++ b/src/main/java/com/ticle/server/user/dto/response/UserInfoResponse.java @@ -0,0 +1,17 @@ +package com.ticle.server.user.dto.response; + +import com.ticle.server.user.domain.User; +import lombok.Builder; + +@Builder +public record UserInfoResponse( + String nickname, + String email +) { + public static UserInfoResponse from(User user) { + return UserInfoResponse.builder() + .nickname(user.getNickName()) + .email(user.getEmail()) + .build(); + } +} diff --git a/src/main/java/com/ticle/server/user/service/UserService.java b/src/main/java/com/ticle/server/user/service/UserService.java index 2a52057..eba8d89 100644 --- a/src/main/java/com/ticle/server/user/service/UserService.java +++ b/src/main/java/com/ticle/server/user/service/UserService.java @@ -6,26 +6,25 @@ import com.ticle.server.user.dto.request.LoginRequest; import com.ticle.server.user.dto.request.ProfileUpdateRequest; import com.ticle.server.user.dto.response.JwtTokenResponse; +import com.ticle.server.user.dto.response.UserInfoResponse; import com.ticle.server.user.dto.response.UserResponse; import com.ticle.server.user.exception.InvalidPasswordException; import com.ticle.server.user.exception.UserNotFoundException; +import com.ticle.server.user.exception.UserNotLoginException; import com.ticle.server.user.jwt.CustomUserDetails; import com.ticle.server.user.jwt.ExpireTime; import com.ticle.server.user.jwt.JwtTokenProvider; -import com.ticle.server.user.redis.CacheNames; import com.ticle.server.user.redis.RedisDao; import com.ticle.server.user.repository.UserRepository; import jakarta.servlet.http.HttpServletRequest; import jakarta.transaction.Transactional; import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; -import org.springframework.cache.annotation.CacheEvict; -import org.springframework.cache.annotation.Cacheable; +import org.apache.commons.lang3.ObjectUtils; import org.springframework.http.ResponseEntity; import org.springframework.security.authentication.UsernamePasswordAuthenticationToken; import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder; import org.springframework.security.core.Authentication; -import org.springframework.security.core.context.SecurityContextHolder; import org.springframework.security.crypto.password.PasswordEncoder; import org.springframework.stereotype.Service; @@ -34,6 +33,7 @@ import java.util.Optional; import static com.ticle.server.user.exception.errorcode.UserErrorCode.USER_NOT_FOUND; +import static com.ticle.server.user.exception.errorcode.UserErrorCode.USER_NOT_LOGIN; @Service @RequiredArgsConstructor @@ -47,7 +47,6 @@ public class UserService { private final RedisDao redisDao; -// @Cacheable(cacheNames = CacheNames.LOGINUSER, key = "#p0.email()", unless = "#result== null") @Transactional public JwtTokenResponse signIn(LoginRequest loginRequest){ String email = loginRequest.email(); @@ -59,7 +58,6 @@ public JwtTokenResponse signIn(LoginRequest loginRequest){ UsernamePasswordAuthenticationToken authenticationToken = new UsernamePasswordAuthenticationToken(email,password); Authentication authentication = authenticationManagerBuilder.getObject().authenticate(authenticationToken); -// SecurityContextHolder.getContext().setAuthentication(authentication); JwtTokenResponse jwtTokenResponse = jwtTokenProvider.generateToken(authentication); redisDao.setRefreshToken(email, jwtTokenResponse.getRefreshToken(), ExpireTime.REFRESH_TOKEN_EXPIRE_TIME); @@ -79,7 +77,6 @@ public UserResponse signUp(JoinRequest joinRequest){ } -// @CacheEvict(cacheNames = CacheNames.USERBYEMAIL, key = "#p1") @Transactional public ResponseEntity logout(CustomUserDetails customUserDetails, HttpServletRequest request) { String accessToken = jwtTokenProvider.resolveToken(request); @@ -119,7 +116,6 @@ public JwtTokenResponse reissueAtk(CustomUserDetails customUserDetails,String re } UsernamePasswordAuthenticationToken authenticationToken = new UsernamePasswordAuthenticationToken(email,null,user.get().getAuthorities()); -// Authentication authentication = authenticationManagerBuilder.getObject().authenticate(authenticationToken); JwtTokenResponse jwtTokenResponse = jwtTokenProvider.generateToken(authenticationToken); String newAccessToken = jwtTokenResponse.getAccessToken(); String newRefreshToken = jwtTokenResponse.getRefreshToken(); @@ -158,4 +154,14 @@ public void deleteUser(CustomUserDetails customUserDetails){ userRepository.delete(user); } + public UserInfoResponse getUserInfo(CustomUserDetails userDetails){ + if (ObjectUtils.isEmpty(userDetails)) { + throw new UserNotLoginException(USER_NOT_LOGIN); + } + + User user = userRepository.findById(userDetails.getUserId()) + .orElseThrow(() -> new UserNotFoundException(USER_NOT_FOUND)); + + return UserInfoResponse.from(user); + } }