diff --git a/src/main/java/com/example/rcp1/domain/user/application/UserService.java b/src/main/java/com/example/rcp1/domain/user/application/UserService.java index 8c77dbc..b7d5a48 100644 --- a/src/main/java/com/example/rcp1/domain/user/application/UserService.java +++ b/src/main/java/com/example/rcp1/domain/user/application/UserService.java @@ -4,8 +4,10 @@ import com.example.rcp1.domain.user.domain.repository.UserRepository; import com.example.rcp1.domain.user.dto.SignInReq; import com.example.rcp1.domain.user.dto.SignUpReq; +import com.example.rcp1.domain.user.dto.UpdateProfileReq; import com.example.rcp1.global.CustomAuthenticationException; import com.example.rcp1.global.config.security.util.JwtUtil; +import io.jsonwebtoken.Jwt; import lombok.RequiredArgsConstructor; import org.mindrot.jbcrypt.BCrypt; import org.springframework.beans.factory.annotation.Value; @@ -72,4 +74,75 @@ public String signIn(SignInReq signInReq) { } + // 유저 정보 논리 삭제 + public String deleteUser(String token) { + + + String subtractedEmail = JwtUtil.getUserEmail(token, secret_key); + + Optional user = userRepository.findByEmail(subtractedEmail); + + User tmpUser = user.get(); + + tmpUser.setStatusD(); + + userRepository.save(tmpUser); + + return ""; + } + + + public User updateProfile(String access_token, UpdateProfileReq updateProfileReq) { + + try { + String email = JwtUtil.getUserEmail(access_token, secret_key); + + Optional user = userRepository.findByEmail(email); + + if (user.isPresent()) { + User userRes = user.get(); + + if (updateProfileReq.getName() != null) { + userRes.setName(updateProfileReq.getName()); + } + + if (updateProfileReq.getPhoneNumber() != null) { + userRes.setPhoneNumber(updateProfileReq.getPhoneNumber()); + } + + if (updateProfileReq.getSpecializedField() != null) { + userRes.setSpecializedField(updateProfileReq.getSpecializedField()); + } + + if (updateProfileReq.getCareer() != null) { + userRes.setCareer(updateProfileReq.getCareer()); + } + + if (updateProfileReq.getPosition() != null) { + userRes.setPosition(updateProfileReq.getPosition()); + } + + if (updateProfileReq.getSchool() != null) { + userRes.setSchool(updateProfileReq.getSchool()); + } + + if (updateProfileReq.getJob() != null) { + userRes.setJob(updateProfileReq.getJob()); + } + + userRepository.save(userRes); + return userRes; + + } else { + return null; + } + } catch (Exception e) { + throw new CustomAuthenticationException("유저 정보 수정에 실패했습니다."); + } + + + } + + + } diff --git a/src/main/java/com/example/rcp1/domain/user/domain/User.java b/src/main/java/com/example/rcp1/domain/user/domain/User.java index 65d1c50..4564ef8 100644 --- a/src/main/java/com/example/rcp1/domain/user/domain/User.java +++ b/src/main/java/com/example/rcp1/domain/user/domain/User.java @@ -78,4 +78,9 @@ public User(Long id, String email, String password, String name, String phoneNum } + // 논리 삭제 상태 수정 + public void setStatusD() { + this.status = "D"; + } + } diff --git a/src/main/java/com/example/rcp1/domain/user/dto/UpdateProfileReq.java b/src/main/java/com/example/rcp1/domain/user/dto/UpdateProfileReq.java new file mode 100644 index 0000000..8d47e37 --- /dev/null +++ b/src/main/java/com/example/rcp1/domain/user/dto/UpdateProfileReq.java @@ -0,0 +1,33 @@ +package com.example.rcp1.domain.user.dto; + +import jakarta.validation.constraints.Email; +import jakarta.validation.constraints.NotBlank; +import lombok.Data; + +import java.time.LocalDateTime; + +@Data +public class UpdateProfileReq { + + @Email + private String email; + + private String password; + + private String name; + + private String phoneNumber; + + + private String specializedField; + + private Long career; + + private String position; + + private String school; + + private String job; + + +} diff --git a/src/main/java/com/example/rcp1/domain/user/presentation/UserController.java b/src/main/java/com/example/rcp1/domain/user/presentation/UserController.java index 5f75e35..b499f73 100644 --- a/src/main/java/com/example/rcp1/domain/user/presentation/UserController.java +++ b/src/main/java/com/example/rcp1/domain/user/presentation/UserController.java @@ -4,6 +4,7 @@ import com.example.rcp1.domain.user.domain.User; import com.example.rcp1.domain.user.dto.SignInReq; import com.example.rcp1.domain.user.dto.SignUpReq; +import com.example.rcp1.domain.user.dto.UpdateProfileReq; import com.example.rcp1.global.BaseResponse; import com.example.rcp1.global.CustomAuthenticationException; import com.example.rcp1.global.ErrorCode; @@ -42,7 +43,6 @@ public ResponseEntity> signUp(@Valid @RequestBody SignUpReq s public ResponseEntity> signIn(@Valid @RequestBody SignInReq signInReq) { try { String token = userService.signIn(signInReq); - System.out.println("token = " + token); if (token != null) { return ResponseEntity.ok(BaseResponse.success(SuccessCode.SIGNIN_SUCCESS, token)); @@ -68,8 +68,49 @@ public ResponseEntity writeReview(Authentication authentication) { return ResponseEntity.ok().body(authentication.getName() + "님의 글작성이 완료되었습니다."); } + @PostMapping("/write2") + public ResponseEntity writeReview2(@RequestHeader("Authorization") String Authorization) { + return ResponseEntity.ok().body(Authorization + "님의 글작성이 완료되었습니다."); + } + + + // 유저 정보 수정 + @PatchMapping("/profile") + public ResponseEntity> updateProfile( + @RequestHeader("Authorization") String Authorization, // 헤더에서 Authorization 값을 받아온다 + @Valid @RequestBody UpdateProfileReq updateProfileReq) { + try { + String access_token = Authorization.substring(7); // Bearer 이후 토큰만 파싱 + + // 토큰에서 이메일 파싱 후 이메일이랑 updateprofilereq 객체랑 같이 서비스에 보낸 후 수정처리 하는 코드 + User user = userService.updateProfile(access_token, updateProfileReq); + + + return ResponseEntity.ok(BaseResponse.success(SuccessCode.UPDATE_PROFILE_SUCCESS, user)); + + } catch (Exception e) { + return ResponseEntity.status(HttpStatus.BAD_REQUEST) + .body(BaseResponse.error(ErrorCode.REQUEST_VALIDATION_EXCEPTION, "유저 정보 수정에 실패했습니다.")); + } + + } + // 유저 정보 탈퇴(논리 삭제) + @PatchMapping("/delete") + public ResponseEntity> deleteUser( + @RequestHeader("Authorization") String authorization + ) { + + try { + String token = authorization.substring(7); + String t = userService.deleteUser(token); + + return ResponseEntity.ok(BaseResponse.success(SuccessCode.LOGICAL_DELETE_SUCCESS)); + } catch (Exception e) { + return ResponseEntity.ok().body(BaseResponse.error(ErrorCode.EXPIRED_TOKEN)); + } + } } diff --git a/src/main/java/com/example/rcp1/global/SuccessCode.java b/src/main/java/com/example/rcp1/global/SuccessCode.java index 57c051b..112b4a6 100644 --- a/src/main/java/com/example/rcp1/global/SuccessCode.java +++ b/src/main/java/com/example/rcp1/global/SuccessCode.java @@ -14,7 +14,9 @@ public enum SuccessCode { // CUSTOM_SUCCESS(OK, "~ 조회에 성공했습니다."), // CUSTOM_CREATED_SUCCESS(CREATED, "~ 생성에 성공했습니다."); SIGNUP_SUCCESS(OK, "회원가입에 성공했습니다."), - SIGNIN_SUCCESS(OK, "로그인에 성공했습니다."); + SIGNIN_SUCCESS(OK, "로그인에 성공했습니다."), + UPDATE_PROFILE_SUCCESS(OK, "프로필이 성공적으로 수정되었습니다."), + LOGICAL_DELETE_SUCCESS(OK, "논리적으로 삭제 되었습니다."); private final HttpStatus httpStatus; private final String message; diff --git a/src/main/java/com/example/rcp1/global/config/security/JwtFilter.java b/src/main/java/com/example/rcp1/global/config/security/JwtFilter.java index 25f68f6..aae49b5 100644 --- a/src/main/java/com/example/rcp1/global/config/security/JwtFilter.java +++ b/src/main/java/com/example/rcp1/global/config/security/JwtFilter.java @@ -48,7 +48,7 @@ protected void doFilterInternal(HttpServletRequest request, HttpServletResponse // 토큰 만료 여부 확인 if (JwtUtil.isExpired(token, secretKey)) { - log.error("토큰이 만료되었습니다."); + log.error("유효하지 않은 액세스 토큰입니다."); filterChain.doFilter(request, response); return; } diff --git a/src/main/java/com/example/rcp1/global/config/security/SecurityConfig.java b/src/main/java/com/example/rcp1/global/config/security/SecurityConfig.java index 8804c65..9eeea5a 100644 --- a/src/main/java/com/example/rcp1/global/config/security/SecurityConfig.java +++ b/src/main/java/com/example/rcp1/global/config/security/SecurityConfig.java @@ -33,7 +33,7 @@ public SecurityFilterChain filterChain(HttpSecurity http) throws Exception { .csrf().disable() .cors().and() .authorizeHttpRequests() - .requestMatchers("/user/signUp", "/user/signIn").permitAll() + .requestMatchers("/user/signUp", "/user/signIn", "/user/delete", "/user/profile").permitAll() .requestMatchers(HttpMethod.POST, "/user/**").authenticated() .and() .sessionManagement()