Skip to content

Commit

Permalink
Merge pull request #323 from Gamegoo-repo/fix/319
Browse files Browse the repository at this point in the history
๐Ÿ›[Fix] ๋น„๋ฐ€๋ฒˆํ˜ธ ์žฌ์„ค์ •์„ ์œ„ํ•œ ์ธ์ฆ์ฝ”๋“œ ๋ฐœ์†ก ๋ฐ ๊ฒ€์ฆ, ์ƒˆ๋กœ์šด ๋น„๋ฐ€๋ฒˆํ˜ธ๋กœ ์ˆ˜์ •
  • Loading branch information
rimi3226 authored Nov 17, 2024
2 parents 442300f + 2b468db commit ccb54f0
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 15 deletions.
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

0 comments on commit ccb54f0

Please sign in to comment.