Skip to content

Commit

Permalink
✨ feat: 중복 이메일 존재 확인 API (#197) (#198)
Browse files Browse the repository at this point in the history
  • Loading branch information
gomin0 authored Sep 12, 2024
1 parent 8f973da commit ac6e969
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import com.capstone.BnagFer.domain.accounts.jwt.dto.JwtDto;
import com.capstone.BnagFer.domain.accounts.jwt.exception.SecurityCustomException;
import com.capstone.BnagFer.domain.accounts.jwt.exception.TokenErrorCode;
import com.capstone.BnagFer.domain.accounts.service.account.AccountsCommonService;
import com.capstone.BnagFer.domain.accounts.service.account.AccountsQueryService;
import com.capstone.BnagFer.domain.accounts.service.account.AccountsService;
import com.capstone.BnagFer.domain.accounts.service.account.KakaoService;
Expand Down Expand Up @@ -37,6 +38,7 @@ public class AccountsController {
private final JwtProvider jwtProvider;
private final KakaoService kakaoService;
private final EmailService emailService;
private final AccountsCommonService accountsCommonService;

@Operation(summary = "일반 로그인", description = "이메일, 비밀번호를 입력받아 로그인을 진행합니다. 이때, FCM토큰을 같이 넘겨줘야 함." +
"반환 값으로 JWT accessToken과 refreshToken이 발급됨. accessToken 값을 Authorize에 인증")
Expand Down Expand Up @@ -152,9 +154,16 @@ public ApiResponse<String> verifyCode(@Valid @RequestBody EmailVerifyDto request
boolean check = emailService.verifyCode(requestDto);
if (check) {
return ApiResponse.onSuccess("인증 완료!");
}
else {
} else {
return ApiResponse.onFailure(HttpStatus.BAD_REQUEST.name(), "인증 실패");
}
}

@Operation(summary = "이메일 사용 가능 여부 확인", description = "이메일을 입력받아 사용 가능한지 여부를 확인합니다.")
@GetMapping("/checkEmail")
public ApiResponse<String> checkEmailAvailability(@RequestParam String email) {
accountsCommonService.checkUserEmail(email);
return ApiResponse.onSuccess("사용 가능한 이메일입니다.");
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.capstone.BnagFer.domain.accounts.entity.User;
import com.capstone.BnagFer.domain.accounts.exception.AccountsExceptionHandler;
import com.capstone.BnagFer.domain.accounts.repository.UserJpaRepository;
import com.capstone.BnagFer.domain.report.entity.UserActivity;
import com.capstone.BnagFer.global.common.ErrorCode;
import lombok.RequiredArgsConstructor;
Expand All @@ -13,6 +14,8 @@
@Service
public class AccountsCommonService {

private final UserJpaRepository userJpaRepository;

public void checkUserProfile(User user) {
if (user.getProfile() == null)
throw new AccountsExceptionHandler(ErrorCode.PROFILE_NOT_EXIST);
Expand All @@ -23,9 +26,9 @@ public void checkUserActivity(User user) {
throw new AccountsExceptionHandler(ErrorCode.USER_IS_BANNED);
}

public void validateStaffAccess(User user) {
if (!user.getIsStaff()) {
throw new AccountsExceptionHandler(ErrorCode.USER_IS_NOT_STAFF);
public void checkUserEmail(String email) {
if (userJpaRepository.existsByEmail(email)) {
throw new AccountsExceptionHandler(ErrorCode.EMAIL_ALREADY_EXIST);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ public class AccountsService {
private final PasswordEncoder passwordEncoder;
private final JwtProvider jwtProvider;
private final RedisUtil redisUtil;
private final AccountsCommonService accountsCommonService;

public UserLoginResponseDto login(UserLoginRequestDto requestDto) {

Expand Down Expand Up @@ -67,9 +68,7 @@ public UserSignupResponseDto signup(UserSignupRequestDto requestDto) {
throw new AccountsExceptionHandler(ErrorCode.PASSWORD_NOT_EQUAL);

// 이메일 중복 확인
if (userJpaRepository.existsByEmail(requestDto.email())) {
throw new AccountsExceptionHandler(ErrorCode.USER_ALREADY_EXIST);
}
accountsCommonService.checkUserEmail(requestDto.email());

String encodedPw = passwordEncoder.encode(requestDto.password());
User user = requestDto.toEntity(encodedPw);
Expand Down Expand Up @@ -144,9 +143,7 @@ public void updateEmail(HttpServletRequest request, User user, ChangeEmailReques
}

// 새 이메일이 이미 사용 중인지 확인
if (userJpaRepository.existsByEmail(requestDto.newEmail())) {
throw new AccountsExceptionHandler(ErrorCode.EMAIL_ALREADY_EXIST);
}
accountsCommonService.checkUserEmail(requestDto.newEmail());

// 이메일 변경
user.updateEmail(requestDto);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ public enum ErrorCode implements BaseErrorCode {
EMAIL_NOT_MATCH(HttpStatus.BAD_REQUEST, "USER414", "이메일이 맞지 않습니다."),
CANNOT_USE_SAME_PASSWORD(HttpStatus.BAD_REQUEST, "USER415", "기존 비밀번호와 동일합니다."),
USER_IS_BANNED(HttpStatus.BAD_REQUEST, "USER416", "사용이 정지된 유저입니다."),
EMAIL_ALREADY_EXIST(HttpStatus.BAD_REQUEST, "USER417", "해당 이메일이 이미 존재합니다."),

// Firebase 관련 에러
FIREBASE_MESSAGING_ERROR(HttpStatus.INTERNAL_SERVER_ERROR, "FIREBASE401", "Firebase 메시징 예외가 발생했습니다."),
Expand All @@ -47,7 +48,6 @@ public enum ErrorCode implements BaseErrorCode {
PROFILE_ALREADY_EXIST(HttpStatus.BAD_REQUEST, "PROFILE402", "프로필이 이미 존재합니다."),
PROFILE_NOT_FOUND(HttpStatus.BAD_REQUEST, "PROFILE403", "해당 프로필이 존재하지 않습니다."),
PROFILE_AND_USER_NOT_MATCHED(HttpStatus.BAD_REQUEST, "PROFILE404", "자신의 프로필이 아닙니다. 권한이 없습니다."),
EMAIL_ALREADY_EXIST(HttpStatus.BAD_REQUEST, "PROFILE404", "해당 이메일이 이미 존재합니다."),

// Tactic 관련 에러
TACTIC_NOT_FOUND(HttpStatus.BAD_REQUEST, "TACTIC401", "전술이 없습니다."),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public class SecurityConfig {

private final String[] swaggerUrls = {"/swagger-ui/**", "/v3/**"};
private final String[] authUrls = {"/", "/accounts/signup/**", "/accounts/social/**", "/accounts/login/**",
"/api/v1/auth", "/oauth/kakao/**", "/accounts/reissue/**", "/accounts/forgotPw/**", "/accounts/email/send-email", "/accounts/email/verify"};
"/api/v1/auth", "/oauth/kakao/**", "/accounts/reissue/**", "/accounts/forgotPw/**", "/accounts/email/send-email", "/accounts/email/verify", "/accounts/checkEmail"};
private final String[] allowedUrls = Stream.concat(Arrays.stream(swaggerUrls), Arrays.stream(authUrls))
.toArray(String[]::new);

Expand Down

0 comments on commit ac6e969

Please sign in to comment.