Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weโ€™ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

๐Ÿ›[Fix] ๋น„๋ฐ€๋ฒˆํ˜ธ ์žฌ์„ค์ •์„ ์œ„ํ•œ ์ธ์ฆ์ฝ”๋“œ ๋ฐœ์†ก ๋ฐ ๊ฒ€์ฆ, ์ƒˆ๋กœ์šด ๋น„๋ฐ€๋ฒˆํ˜ธ๋กœ ์ˆ˜์ • #323

Merged
merged 1 commit into from
Nov 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ public ApiResponse<String> verifyEmail(
@Valid @RequestBody MemberRequest.EmailCodeRequestDTO emailCodeRequestDTO) {
String email = emailCodeRequestDTO.getEmail();
String code = emailCodeRequestDTO.getCode();
authService.verifyEmail(email, code);
authService.verifyCode(email, code);
return ApiResponse.onSuccess("์ธ์ฆ์ฝ”๋“œ ๊ฒ€์ฆ์— ์„ฑ๊ณตํ–ˆ์Šต๋‹ˆ๋‹ค.");
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import com.gamegoo.apiPayload.code.status.ErrorStatus;
import com.gamegoo.apiPayload.exception.handler.MemberHandler;
import com.gamegoo.dto.member.MemberRequest;
import com.gamegoo.service.member.AuthService;
import com.gamegoo.service.member.PasswordService;
import com.gamegoo.util.JWTUtil;
import io.swagger.v3.oas.annotations.Operation;
Expand All @@ -23,6 +24,7 @@
public class PasswordController {

private final PasswordService passwordService;
private final AuthService authService;

@PostMapping("/check")
@Operation(summary = "JWT ํ† ํฐ์ด ํ•„์š”ํ•œ ๋น„๋ฐ€๋ฒˆํ˜ธ ํ™•์ธ API ์ž…๋‹ˆ๋‹ค.", description = "API for checking password with JWT")
Expand All @@ -45,7 +47,7 @@ public ApiResponse<String> checkPasswordWithJWT(
public ApiResponse<String> resetPasswordWithJWT(
@Valid @RequestBody MemberRequest.PasswordRequestJWTDTO passwordRequestDTO) {
Long currentUserId = JWTUtil.getCurrentUserId();
passwordService.updatePassword(currentUserId, passwordRequestDTO.getOldPassword(), passwordRequestDTO.getNewPassword());
passwordService.updatePasswordById(currentUserId, passwordRequestDTO.getOldPassword(), passwordRequestDTO.getNewPassword());

return ApiResponse.onSuccess("๋น„๋ฐ€๋ฒˆํ˜ธ ์žฌ์„ค์ •์„ ์™„๋ฃŒํ–ˆ์Šต๋‹ˆ๋‹ค.");
}
Expand All @@ -54,8 +56,16 @@ public ApiResponse<String> resetPasswordWithJWT(
@Operation(summary = "๋น„๋ฐ€๋ฒˆํ˜ธ ์žฌ์„ค์ • API ์ž…๋‹ˆ๋‹ค.", description = "API for reseting password")
public ApiResponse<String> resetPassword(
@Valid @RequestBody MemberRequest.PasswordRequestDTO passwordRequestDTO) {
// dto
String email = passwordRequestDTO.getEmail();
String verifyCode = passwordRequestDTO.getVerifyCode();
String newPassword = passwordRequestDTO.getNewPassword();

passwordService.updatePasswordWithEmail(passwordRequestDTO.getEmail());
// ์ธ์ฆ์ฝ”๋“œ ๊ฒ€์ฆ
authService.verifyCode(email,verifyCode);

// ๋น„๋ฐ€๋ฒˆํ˜ธ ์žฌ์„ค์ •
passwordService.updatePasswordWithEmail(email, newPassword);

return ApiResponse.onSuccess("๋น„๋ฐ€๋ฒˆํ˜ธ ์žฌ์„ค์ •์„ ์™„๋ฃŒํ–ˆ์Šต๋‹ˆ๋‹ค.");
}
Expand Down
6 changes: 6 additions & 0 deletions src/main/java/com/gamegoo/dto/member/MemberRequest.java
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,12 @@ public static class PasswordRequestDTO {
@NotBlank(message = "Email์€ ๋น„์›Œ๋‘˜ ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค.")
String email;

@NotBlank(message = "newPassword๋Š” ๋น„์›Œ๋‘˜ ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค.")
String newPassword;

@NotBlank(message = "verifyCode๋Š” ๋น„์›Œ๋‘˜ ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค.")
String verifyCode;

}


Expand Down
2 changes: 1 addition & 1 deletion src/main/java/com/gamegoo/service/member/AuthService.java
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,7 @@ public MemberResponse.RefreshTokenResponseDTO verifyRefreshToken(String refresh_
* @param email
* @param code
*/
public void verifyEmail(String email, String code) {
public void verifyCode(String email, String code) {
// ์ด๋ฉ”์ผ๋กœ ๋ณด๋‚ธ ์ธ์ฆ ์ฝ”๋“œ ์ค‘ ๊ฐ€์žฅ ์ตœ๊ทผ์˜ ๋ฐ์ดํ„ฐ๋ฅผ ๋ถˆ๋Ÿฌ์˜ด
EmailVerifyRecord emailVerifyRecord = emailVerifyRecordRepository.findByEmailOrderByUpdatedAtDesc(
email, PageRequest.of(0, 1))
Expand Down
16 changes: 5 additions & 11 deletions src/main/java/com/gamegoo/service/member/PasswordService.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import com.gamegoo.apiPayload.exception.handler.MemberHandler;
import com.gamegoo.domain.member.Member;
import com.gamegoo.repository.member.MemberRepository;
import com.gamegoo.util.CodeGeneratorUtil;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.mail.javamail.JavaMailSender;
Expand Down Expand Up @@ -44,7 +43,7 @@ public boolean checkPasswordById(Long userId, String password) {
* @param userId
* @param newPassword
*/
public void updatePassword(Long userId, String oldPassword, String newPassword) {
public void updatePasswordById(Long userId, String oldPassword, String newPassword) {
// jwt ํ† ํฐ์œผ๋กœ ๋ฉค๋ฒ„ ์ฐพ๊ธฐ
Member member = memberRepository.findById(userId)
.orElseThrow(() -> new MemberHandler(ErrorStatus.MEMBER_NOT_FOUND));
Expand All @@ -66,20 +65,15 @@ public void updatePassword(Long userId, String oldPassword, String newPassword)
*
* @param email
*/
public void updatePasswordWithEmail(String email) {
public void updatePasswordWithEmail(String email, String newPassword) {
// email์œผ๋กœ ๋ฉค๋ฒ„ ์ฐพ๊ธฐ
Member member = memberRepository.findByEmail(email)
.orElseThrow(() -> new MemberHandler(ErrorStatus.MEMBER_NOT_FOUND));

// ๋žœ๋ค ์ž„์‹œ ๋น„๋ฐ€๋ฒˆํ˜ธ ์ƒ์„ฑ
String tempPassword = CodeGeneratorUtil.generatePasswordRandomCode();
// ๋น„๋ฐ€๋ฒˆํ˜ธ ์žฌ์„ค์ •
member.updatePassword(bCryptPasswordEncoder.encode(newPassword));
memberRepository.save(member);

// ์ด๋ฉ”์ผ ์ „์†ก
sendEmailInternal(email,tempPassword);

// ๋น„๋ฐ€๋ฒˆํ˜ธ ์žฌ์„ค์ •
member.updatePassword(bCryptPasswordEncoder.encode(tempPassword));
memberRepository.save(member);
}

/**
Expand Down
Loading