Skip to content

Commit

Permalink
✨ Feat: 유저 프로필 정보 가져오기
Browse files Browse the repository at this point in the history
  • Loading branch information
Amepistheo committed Jul 25, 2024
1 parent 5363226 commit 8e0e401
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 22 deletions.
26 changes: 12 additions & 14 deletions src/main/java/com/ticle/server/user/controller/UserController.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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")
Expand All @@ -50,16 +47,6 @@ public ResponseEntity<ResponseTemplate<Object>> signUp(@RequestBody JoinRequest
.body(ResponseTemplate.from(savedUserDto));
}

// @Operation(summary = "관심직무 선택", description = "회원가입 이후 ")
// @PostMapping("/sign-up/category")
// public ResponseEntity<ResponseTemplate<Object>> 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<ResponseTemplate<Object>> logout(@AuthenticationPrincipal CustomUserDetails userDetails, HttpServletRequest request){
Expand Down Expand Up @@ -111,4 +98,15 @@ public ResponseEntity<ResponseTemplate<Object>> deleteUser(@AuthenticationPrinci
.body(ResponseTemplate.from(customUserDetails.getUserId() + "님의 회원정보가 삭제되었습니다.\n" ));
}

@Operation(summary = "유저 정보 조회", description = "유저의 정보를 조회")
@GetMapping("/profile")
public ResponseEntity<ResponseTemplate<Object>> getUserInfo(
@AuthenticationPrincipal CustomUserDetails userDetails){

UserInfoResponse response = userService.getUserInfo(userDetails);

return ResponseEntity
.status(OK)
.body(ResponseTemplate.from(response));
}
}
Original file line number Diff line number Diff line change
@@ -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();
}
}
22 changes: 14 additions & 8 deletions src/main/java/com/ticle/server/user/service/UserService.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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
Expand All @@ -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();
Expand All @@ -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);
Expand All @@ -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);
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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);
}
}

0 comments on commit 8e0e401

Please sign in to comment.