Skip to content

Commit

Permalink
feat: 닉네임 및 이메일 중복 방지 기능 추가
Browse files Browse the repository at this point in the history
  • Loading branch information
yunhacandy committed Aug 15, 2024
1 parent a42b5e5 commit ed4b56e
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,14 @@

import cotato.growingpain.auth.dto.request.ChangePasswordRequest;
import cotato.growingpain.auth.dto.request.CompleteSignupRequest;
import cotato.growingpain.auth.dto.request.DuplicateCheckRequest;
import cotato.growingpain.auth.dto.request.LoginRequest;
import cotato.growingpain.auth.dto.request.LogoutRequest;
import cotato.growingpain.auth.dto.request.ResetPasswordRequest;
import cotato.growingpain.auth.dto.response.DuplicateCheckResponse;
import cotato.growingpain.auth.dto.response.ResetPasswordResponse;
import cotato.growingpain.auth.service.AuthService;
import cotato.growingpain.auth.service.ValidateService;
import cotato.growingpain.common.Response;
import cotato.growingpain.security.jwt.Token;
import cotato.growingpain.security.jwt.dto.request.ReissueRequest;
Expand All @@ -23,6 +26,7 @@
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestHeader;
Expand All @@ -38,6 +42,7 @@
public class AuthController {

private final AuthService authService;
private final ValidateService validateService;

@Operation(summary = "일반 로그인", description = "회원가입 및 로그인을 위한 메소드")
@ApiResponse(content = @Content(schema = @Schema(implementation = Token.class)))
Expand Down Expand Up @@ -94,4 +99,34 @@ public Response<?> changePassword(@RequestBody @Valid ChangePasswordRequest requ
authService.changePassword(request, memberId);
return Response.createSuccessWithNoData("비밀번호 변경 완료");
}

@Operation(summary = "이메일 중복 검증", description = "회원가입 시 기존 사용자와 이메일이 중복되는지 확인하는 메소드")
@ApiResponse(content = @Content(schema = @Schema(implementation = DuplicateCheckResponse.class)))
@GetMapping("/validate/email")
@ResponseStatus(HttpStatus.OK)
public Response<DuplicateCheckResponse> checkDuplicateEmail(@RequestBody DuplicateCheckRequest request) {

boolean isDuplicate = validateService.isDuplicateEmail(request.value());
DuplicateCheckResponse response = new DuplicateCheckResponse(isDuplicate);
if (isDuplicate) {
return Response.createSuccess("이미 사용 중인 이메일입니다.", response);
} else {
return Response.createSuccess("사용 가능한 이메일입니다.", response);
}
}

@Operation(summary = "닉네임 중복 검증", description = "추가 정보 입력 시 기존 사용자와 닉네임이 중복되는지 확인하는 메소드")
@ApiResponse(content = @Content(schema = @Schema(implementation = DuplicateCheckResponse.class)))
@GetMapping("/validate/nickname")
@ResponseStatus(HttpStatus.OK)
public Response<DuplicateCheckResponse> checkDuplicateNickname(@RequestBody DuplicateCheckRequest request) {
boolean isDuplicate = validateService.isDuplicateNickname(request.value());
DuplicateCheckResponse response = new DuplicateCheckResponse(isDuplicate);

if (isDuplicate) {
return Response.createSuccess("이미 사용 중인 닉네임입니다.", response);
} else {
return Response.createSuccess("사용 가능한 닉네임입니다.", response);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package cotato.growingpain.auth.dto.request;

public record DuplicateCheckRequest(
String value
) {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package cotato.growingpain.auth.dto.response;

public record DuplicateCheckResponse(
Boolean isDuplicate
) {
}
13 changes: 10 additions & 3 deletions src/main/java/cotato/growingpain/auth/service/ValidateService.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ public class ValidateService {
private static final String PASSWORD_PATTERN = "^(?=.*[A-Za-z])(?=.*\\d)[A-Za-z\\d]{8,16}$";
private final MemberRepository memberRepository;


public void checkPasswordPattern(String password) {
Pattern pattern = Pattern.compile(PASSWORD_PATTERN);
Matcher matcher = pattern.matcher(password);
Expand All @@ -37,16 +36,24 @@ public boolean isValidPassword(String password) {
}

public void checkDuplicateNickName(String name) {
if (memberRepository.findByName(name).isPresent()) {
if (isDuplicateNickname(name)) {
log.error("[회원 가입 실패]: 존재하는 닉네임 " + name);
throw new AppException(ErrorCode.NICKNAME_DUPLICATED);
}
}

public void checkDuplicateEmail(@NotBlank(message = "로그인 할 아이디를 입력해주세요") String email) {
if (memberRepository.findByEmail(email).isPresent()) {
if (isDuplicateEmail(email)) {
log.error("[회원 가입 실패]: 중복된 이메일 " + email);
throw new AppException(ErrorCode.EMAIL_DUPLICATED);
}
}

public boolean isDuplicateEmail(String email) {
return memberRepository.findByEmail(email).isPresent();
}

public boolean isDuplicateNickname(String nickName) {
return memberRepository.findByName(nickName).isPresent();
}
}

0 comments on commit ed4b56e

Please sign in to comment.