diff --git a/src/main/java/com/capstone/BnagFer/domain/accounts/controller/AccountsController.java b/src/main/java/com/capstone/BnagFer/domain/accounts/controller/AccountsController.java index 168d081b..712be348 100644 --- a/src/main/java/com/capstone/BnagFer/domain/accounts/controller/AccountsController.java +++ b/src/main/java/com/capstone/BnagFer/domain/accounts/controller/AccountsController.java @@ -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; @@ -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에 인증") @@ -152,9 +154,16 @@ public ApiResponse 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 checkEmailAvailability(@RequestParam String email) { + accountsCommonService.checkUserEmail(email); + return ApiResponse.onSuccess("사용 가능한 이메일입니다."); + } + } diff --git a/src/main/java/com/capstone/BnagFer/domain/accounts/service/account/AccountsCommonService.java b/src/main/java/com/capstone/BnagFer/domain/accounts/service/account/AccountsCommonService.java index 87f7599c..cc0999de 100644 --- a/src/main/java/com/capstone/BnagFer/domain/accounts/service/account/AccountsCommonService.java +++ b/src/main/java/com/capstone/BnagFer/domain/accounts/service/account/AccountsCommonService.java @@ -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; @@ -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); @@ -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); } } } \ No newline at end of file diff --git a/src/main/java/com/capstone/BnagFer/domain/accounts/service/account/AccountsService.java b/src/main/java/com/capstone/BnagFer/domain/accounts/service/account/AccountsService.java index 8344ab0f..32e44e86 100644 --- a/src/main/java/com/capstone/BnagFer/domain/accounts/service/account/AccountsService.java +++ b/src/main/java/com/capstone/BnagFer/domain/accounts/service/account/AccountsService.java @@ -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) { @@ -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); @@ -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); diff --git a/src/main/java/com/capstone/BnagFer/global/common/ErrorCode.java b/src/main/java/com/capstone/BnagFer/global/common/ErrorCode.java index adbdf5c0..a47994c2 100644 --- a/src/main/java/com/capstone/BnagFer/global/common/ErrorCode.java +++ b/src/main/java/com/capstone/BnagFer/global/common/ErrorCode.java @@ -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 메시징 예외가 발생했습니다."), @@ -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", "전술이 없습니다."), diff --git a/src/main/java/com/capstone/BnagFer/global/config/SecurityConfig.java b/src/main/java/com/capstone/BnagFer/global/config/SecurityConfig.java index d65c908c..53d2a8a9 100644 --- a/src/main/java/com/capstone/BnagFer/global/config/SecurityConfig.java +++ b/src/main/java/com/capstone/BnagFer/global/config/SecurityConfig.java @@ -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);