Skip to content

Commit

Permalink
Merge pull request #108 from reading-log/develop
Browse files Browse the repository at this point in the history
MAIN MERGE [이메일 로직 수정]
  • Loading branch information
enjoy89 authored Apr 24, 2024
2 parents 583c1fa + 062477a commit f78decb
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@

import com.api.readinglog.common.exception.ErrorCode;
import com.api.readinglog.common.exception.custom.EmailException;
import com.api.readinglog.common.exception.custom.MemberException;
import com.api.readinglog.common.redis.service.RedisService;
import com.api.readinglog.domain.member.entity.Member;
import com.api.readinglog.domain.member.entity.MemberRole;
import com.api.readinglog.domain.member.service.MemberService;
import jakarta.mail.MessagingException;
import jakarta.mail.internet.MimeMessage;
Expand Down Expand Up @@ -43,11 +45,11 @@ public void sendAuthCode(String toEmail) {
}

@Async
public void sendTemporaryPassword(Long memberId, String toEmail) {
public void sendTemporaryPassword(String toEmail) {
String tempPassword = createRandomCode();
sendEmail(toEmail, tempPassword, "[리딩 로그] 임시 비밀번호", "tempPassword.html");

Member member = memberService.getMemberById(memberId);
Member member = memberService.getMemberByEmailAndRole(toEmail, MemberRole.MEMBER_NORMAL);
member.updatePassword(passwordEncoder.encode(tempPassword));
}

Expand All @@ -67,12 +69,18 @@ public void sendEmail(String toEmail, String code, String subject, String templa
}
}

public void verifyAuthCode(String email, String authCode) {
public void validateAuthCode(String email, String authCode) {
findByEmailAndAuthCode(authCode)
.filter(e -> e.equals(email))
.orElseThrow(() -> new EmailException(ErrorCode.INVALID_AUTH_CODE));
}

public void validateMember(String email) {
if (!isMemberExists(email)) {
throw new EmailException(ErrorCode.NOT_FOUND_MEMBER);
}
}

// 인증번호 및 임시 비밀번호 생성
private String createRandomCode() {
Random random = new Random();
Expand Down Expand Up @@ -108,5 +116,9 @@ private Optional<String> findByEmailAndAuthCode(String authCode) {
Object email = redisService.getData(authCode);
return Optional.ofNullable(email != null ? email.toString() : null);
}

private boolean isMemberExists(String email) {
return (memberService.getMemberByEmailAndRole(email, MemberRole.MEMBER_NORMAL)) != null;
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -198,13 +198,15 @@ public Response<Void> updatePassword(@AuthenticationPrincipal CustomUserDetail u
return Response.success(HttpStatus.OK, "비밀번호 변경 성공");
}

@Operation(summary = "이메일 인증 코드 전송", description = "사용자 이메일로 인증 코드를 전송합니다.")
@Operation(summary = "이메일 인증 코드 전송", description = "사용자 이메일로 인증 코드를 전송합니다. 회원 존재 유무도 함께 검사합니다.")
@ApiResponses(value = {
@ApiResponse(responseCode = "200", description = "이메일 인증 코드 전송 완료",
content = {@Content(schema = @Schema(implementation = Response.class))})
})
@PostMapping("/send-authCode")
public Response<Void> sendEmailAuthCode(@RequestBody @Valid EmailRequest request) {
// 회원 인증 후 인증 코드 전송
emailService.validateMember(request.getEmail());
emailService.sendAuthCode(request.getEmail());
return Response.success(HttpStatus.OK, "이메일 인증 코드 전송 완료");
}
Expand All @@ -217,7 +219,7 @@ public Response<Void> sendEmailAuthCode(@RequestBody @Valid EmailRequest request
})
@PostMapping("/verify-authCode")
public Response<Void> verifyAuthCode(@RequestBody @Valid AuthCodeVerificationRequest request) {
emailService.verifyAuthCode(request.getEmail(), request.getAuthCode());
emailService.validateAuthCode(request.getEmail(), request.getAuthCode());
return Response.success(HttpStatus.OK, "이메일 인증 성공");
}

Expand All @@ -227,9 +229,8 @@ public Response<Void> verifyAuthCode(@RequestBody @Valid AuthCodeVerificationReq
content = {@Content(schema = @Schema(implementation = Response.class))})
})
@PostMapping("/send-temporaryPassword")
public Response<Void> sendEmailTempPassword(@AuthenticationPrincipal CustomUserDetail user,
@RequestBody @Valid EmailRequest request) {
emailService.sendTemporaryPassword(user.getId(), request.getEmail());
public Response<Void> sendEmailTempPassword(@RequestBody @Valid EmailRequest request) {
emailService.sendTemporaryPassword(request.getEmail());
return Response.success(HttpStatus.OK, "임시 비밀번호 전송 완료");
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
Expand Down Expand Up @@ -71,7 +70,7 @@ public JwtToken login(LoginRequest request) {

public Member getMemberByEmailAndRole(String email, MemberRole role) {
return memberRepository.findByEmailAndRole(email, role)
.orElseThrow(() -> new UsernameNotFoundException(ErrorCode.NOT_FOUND_MEMBER.getMessage()));
.orElseThrow(() -> new MemberException(ErrorCode.NOT_FOUND_MEMBER));
}

public Member getMemberById(Long memberId) {
Expand Down

0 comments on commit f78decb

Please sign in to comment.