Skip to content

Commit

Permalink
refactor: 닉네임 중복, 이메일형식 검증 추가 (#276)
Browse files Browse the repository at this point in the history
* refactor 닉네임 중복, 이메일형식 검증 추가

* Exception status code 수정, 백준아이디 유효성 검증

* check함수로 빼기..

* 비밀번호 형식 검증 추가

* test 살짝 수정..

* 정규식 주석 달기
  • Loading branch information
s-hwan authored Jan 15, 2025
1 parent fa1e842 commit cb66af2
Show file tree
Hide file tree
Showing 5 changed files with 83 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,9 @@
import com.gamzabat.algohub.feature.solution.exception.SolutionValidationException;
import com.gamzabat.algohub.feature.user.exception.BOJServerErrorException;
import com.gamzabat.algohub.feature.user.exception.CheckBjNicknameValidationException;
import com.gamzabat.algohub.feature.user.exception.CheckEmailFormException;
import com.gamzabat.algohub.feature.user.exception.CheckNicknameValidationException;
import com.gamzabat.algohub.feature.user.exception.CheckPasswordFormException;
import com.gamzabat.algohub.feature.user.exception.UncorrectedPasswordException;

@ControllerAdvice
Expand Down Expand Up @@ -169,4 +171,16 @@ protected ResponseEntity<ErrorResponse> handler(AwsS3Exception e) {
return ResponseEntity.internalServerError()
.body(new ErrorResponse(HttpStatus.INTERNAL_SERVER_ERROR.value(), e.getError(), null));
}

@ExceptionHandler(CheckEmailFormException.class)
protected ResponseEntity<ErrorResponse> handler(CheckEmailFormException e) {
return ResponseEntity.status(e.getCode())
.body(new ErrorResponse(e.getCode(), e.getErrors(), null));
}

@ExceptionHandler(CheckPasswordFormException.class)
protected ResponseEntity<ErrorResponse> handler(CheckPasswordFormException e) {
return ResponseEntity.status(e.getCode())
.body(new ErrorResponse(e.getCode(), e.getErrors(), null));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package com.gamzabat.algohub.feature.user.exception;

import lombok.Getter;

@Getter
public class CheckEmailFormException extends RuntimeException {
private final int code;
private final String errors;

public CheckEmailFormException(int code, String errors) {
this.code = code;
this.errors = errors;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package com.gamzabat.algohub.feature.user.exception;

import lombok.Getter;

@Getter
public class CheckPasswordFormException extends RuntimeException {
private final int code;
private final String errors;

public CheckPasswordFormException(int code, String errors) {
this.code = code;
this.errors = errors;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,9 @@
import com.gamzabat.algohub.feature.user.dto.UserInfoResponse;
import com.gamzabat.algohub.feature.user.exception.BOJServerErrorException;
import com.gamzabat.algohub.feature.user.exception.CheckBjNicknameValidationException;
import com.gamzabat.algohub.feature.user.exception.CheckEmailFormException;
import com.gamzabat.algohub.feature.user.exception.CheckNicknameValidationException;
import com.gamzabat.algohub.feature.user.exception.CheckPasswordFormException;
import com.gamzabat.algohub.feature.user.exception.UncorrectedPasswordException;
import com.gamzabat.algohub.feature.user.repository.UserRepository;

Expand All @@ -65,6 +67,11 @@ public class UserService {
@Transactional
public void register(RegisterRequest request, MultipartFile profileImage) {
checkEmailDuplication(request.email());
checkNickname(request.nickname());
checkEmailForm(request.email());
checkBjNickname(request.bjNickname());
checkPasswordForm(request.password());

String encodedPassword = passwordEncoder.encode(request.password());

User user = userRepository.save(User.builder()
Expand Down Expand Up @@ -250,4 +257,37 @@ public TokenResponse reissueToken(ReissueTokenRequest request) {
log.info("success to reissue tokens");
return response;
}

private void checkEmailForm(String email) {
if (!isValidEmailForm(email))
throw new CheckEmailFormException(HttpStatus.BAD_REQUEST.value(), "이메일 형식이 아닙니다");
}

private boolean isValidEmailForm(String email) {

String EMAIL_REGEX = "^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\\.[a-zA-Z]{2,}$"; // 문자 사이에 @를 포함하고 최상위 도메인은 2글자 이상이어야 함
Pattern EMAIL_PATTERN = Pattern.compile(EMAIL_REGEX);

if (email == null || email.isEmpty()) {
return false;
}
return EMAIL_PATTERN.matcher(email).matches();
}

private void checkPasswordForm(String password) {
if (!isValidPasswordForm(password))
throw new CheckPasswordFormException(HttpStatus.BAD_REQUEST.value(), "올바르지 않은 비밀번호 형식입니다");
}

private boolean isValidPasswordForm(String password) {

String passwordPattern = "^(?=.*[A-Za-z])(?=.*\\d)(?=.*[~!@#$%^&*])[A-Za-z\\d~!@#$%^&*]{8,15}$"; // 영문,숫자,특수문자 만으로 이루어져야 하며 모두 포함하여야 하고 8~15글자 사이여야 함

if (password == null || password.isEmpty()) {
return false;
}
return password.matches(passwordPattern);

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ class UserServiceTest {
private ArgumentCaptor<User> userCaptor;

private final String email = "[email protected]";
private final String password = "password";
private final String password = "password1!";
private final String nickname = "nickname";
private final String encoded = "encoded";
private final String imageUrl = "[email protected]_image.jpg";
Expand Down

0 comments on commit cb66af2

Please sign in to comment.