Skip to content

Commit

Permalink
[feat] #10 인증번호 전송 시 이메일 중복 확인
Browse files Browse the repository at this point in the history
  • Loading branch information
ziiyouth committed Dec 30, 2023
1 parent a0b7cbb commit ffe5ceb
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,5 @@
public interface UserRepository extends JpaRepository<User, Long> {

Optional<User> findByEmail(String email);
boolean existsByEmail(String email);
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,14 @@
import org.gachon.checkmate.domain.member.repository.UserRepository;
import org.gachon.checkmate.global.config.auth.jwt.JwtProvider;
import org.gachon.checkmate.global.config.mail.MailProvider;
import org.gachon.checkmate.global.error.exception.ConflictException;
import org.gachon.checkmate.global.error.exception.EntityNotFoundException;
import org.gachon.checkmate.global.error.exception.UnauthorizedException;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import static org.gachon.checkmate.global.error.ErrorCode.INVALID_PASSWORD;
import static org.gachon.checkmate.global.error.ErrorCode.USER_NOT_FOUND;
import static org.gachon.checkmate.global.error.ErrorCode.*;

@Slf4j
@RequiredArgsConstructor
Expand All @@ -33,6 +33,7 @@ public class MemberService {
private final UserRepository userRepository;

public EmailResponseDto sendMail(EmailPostRequestDto emailPostRequestDto) {
checkDuplicateEmail(emailPostRequestDto.email());
String authNum = mailProvider.sendMail(emailPostRequestDto.email(), "email");
return new EmailResponseDto(authNum);
}
Expand All @@ -44,7 +45,7 @@ public MemberSignUpResponseDto signUp(MemberSignUpRequestDto memberSignUpRequest
return MemberSignUpResponseDto.of(newMemberId, memberSignUpRequestDto.name(), accessToken, refreshToken);
}

public MemberSignInResponseDto signIn(MemberSignInRequestDto memberSignInRequestDto){
public MemberSignInResponseDto signIn(MemberSignInRequestDto memberSignInRequestDto) {
User user = getUserFromEmail(memberSignInRequestDto.email());
if (!authenticatePassword(memberSignInRequestDto.password(), user.getPassword())) {
throw new UnauthorizedException(INVALID_PASSWORD);
Expand All @@ -54,6 +55,12 @@ public MemberSignInResponseDto signIn(MemberSignInRequestDto memberSignInRequest
return MemberSignInResponseDto.of(user.getId(), accessToken, refreshToken);
}

private void checkDuplicateEmail(String email) {
if (userRepository.existsByEmail(email)) {
throw new ConflictException(DUPLICATE_EMAIL);
}
}

private String issueNewToken(Long memberId) {
return jwtProvider.getIssueToken(memberId, true);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ public enum ErrorCode {
* 409 Conflict
*/
CONFLICT(HttpStatus.CONFLICT, "이미 존재하는 리소스입니다."),
DUPLICATE_EMAIL(HttpStatus.CONFLICT, "이미 가입된 이메일입니다."),

/**
* 500 Internal Server Error
Expand Down

0 comments on commit ffe5ceb

Please sign in to comment.