Skip to content

Commit 971dc60

Browse files
committed
refactor: 이메일 로그인 코드 가독성 개선
1 parent 930a832 commit 971dc60

File tree

3 files changed

+15
-15
lines changed

3 files changed

+15
-15
lines changed
Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,18 @@
11
package com.example.solidconnection.auth.service;
22

3-
import static com.example.solidconnection.common.exception.ErrorCode.USER_NOT_FOUND;
3+
import static com.example.solidconnection.common.exception.ErrorCode.SIGN_IN_FAILED;
44

55
import com.example.solidconnection.auth.dto.EmailSignInRequest;
66
import com.example.solidconnection.auth.dto.SignInResponse;
77
import com.example.solidconnection.common.exception.CustomException;
88
import com.example.solidconnection.siteuser.domain.AuthType;
99
import com.example.solidconnection.siteuser.domain.SiteUser;
1010
import com.example.solidconnection.siteuser.repository.SiteUserRepository;
11-
import java.util.Optional;
1211
import lombok.RequiredArgsConstructor;
1312
import org.springframework.security.crypto.password.PasswordEncoder;
1413
import org.springframework.stereotype.Service;
14+
import org.springframework.transaction.annotation.Transactional;
1515

16-
/*
17-
* 보안을 위해 이메일과 비밀번호 중 무엇이 틀렸는지 구체적으로 응답하지 않는다.
18-
* */
1916
@Service
2017
@RequiredArgsConstructor
2118
public class EmailSignInService {
@@ -24,19 +21,21 @@ public class EmailSignInService {
2421
private final SiteUserRepository siteUserRepository;
2522
private final PasswordEncoder passwordEncoder;
2623

24+
@Transactional(readOnly = true)
2725
public SignInResponse signIn(EmailSignInRequest signInRequest) {
28-
Optional<SiteUser> optionalSiteUser = siteUserRepository.findByEmailAndAuthType(signInRequest.email(), AuthType.EMAIL);
29-
if (optionalSiteUser.isPresent()) {
30-
SiteUser siteUser = optionalSiteUser.get();
31-
validatePassword(signInRequest.password(), siteUser.getPassword());
32-
return signInService.signIn(siteUser);
33-
}
34-
throw new CustomException(USER_NOT_FOUND, "이메일과 비밀번호를 확인해주세요.");
26+
SiteUser siteUser = getEmailMatchingUserOrThrow(signInRequest.email());
27+
validatePassword(signInRequest.password(), siteUser.getPassword());
28+
return signInService.signIn(siteUser);
29+
}
30+
31+
private SiteUser getEmailMatchingUserOrThrow(String email) {
32+
return siteUserRepository.findByEmailAndAuthType(email, AuthType.EMAIL)
33+
.orElseThrow(() -> new CustomException(SIGN_IN_FAILED));
3534
}
3635

3736
private void validatePassword(String rawPassword, String encodedPassword) {
3837
if (!passwordEncoder.matches(rawPassword, encodedPassword)) {
39-
throw new CustomException(USER_NOT_FOUND, "이메일과 비밀번호를 확인해주세요.");
38+
throw new CustomException(SIGN_IN_FAILED);
4039
}
4140
}
4241
}

src/main/java/com/example/solidconnection/common/exception/ErrorCode.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ public enum ErrorCode {
5656
ACCESS_TOKEN_EXPIRED(HttpStatus.UNAUTHORIZED.value(), "액세스 토큰이 만료되었습니다. 재발급 api를 호출해주세요."),
5757
REFRESH_TOKEN_EXPIRED(HttpStatus.UNAUTHORIZED.value(), "리프레시 토큰이 만료되었습니다. 다시 로그인을 진행해주세요."),
5858
ACCESS_DENIED(HttpStatus.FORBIDDEN.value(), "접근 권한이 없습니다."),
59+
SIGN_IN_FAILED(HttpStatus.UNAUTHORIZED.value(), "로그인에 실패했습니다. 이메일과 비밀번호를 확인해주세요."),
5960

6061
// s3
6162
S3_SERVICE_EXCEPTION(HttpStatus.BAD_REQUEST.value(), "S3 서비스 에러 발생"),

src/test/java/com/example/solidconnection/auth/service/EmailSignInServiceTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ class 로그인에_실패한다 {
5555
// when & then
5656
assertThatCode(() -> emailSignInService.signIn(signInRequest))
5757
.isInstanceOf(CustomException.class)
58-
.hasMessageContaining(ErrorCode.USER_NOT_FOUND.getMessage());
58+
.hasMessageContaining(ErrorCode.SIGN_IN_FAILED.getMessage());
5959
}
6060

6161
@Test
@@ -68,7 +68,7 @@ class 로그인에_실패한다 {
6868
// when & then
6969
assertThatCode(() -> emailSignInService.signIn(signInRequest))
7070
.isInstanceOf(CustomException.class)
71-
.hasMessageContaining(ErrorCode.USER_NOT_FOUND.getMessage());
71+
.hasMessageContaining(ErrorCode.SIGN_IN_FAILED.getMessage());
7272
}
7373
}
7474
}

0 commit comments

Comments
 (0)