Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

✨ 아이디 존재 여부 확인 #44

Merged
merged 2 commits into from
Dec 15, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 13 additions & 1 deletion src/main/java/com/kcy/fitapet/domain/member/api/AccountApi.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@Tag(name = "프로필 API")
@RestController
@Slf4j
Expand All @@ -38,9 +40,19 @@ public ResponseEntity<?> getProfile(@AuthenticationPrincipal CustomUserDetails u
return ResponseEntity.ok(SuccessResponse.from(member));
}

@Operation(summary = "ID 존재 확인")
@GetMapping("/exists")
@Parameters({
@Parameter(name = "uid", description = "확인할 ID", required = true)
})
public ResponseEntity<?> getExistsUid(@RequestParam("uid") @NotBlank String uid) {
boolean exists = memberAccountService.existsUid(uid);
return ResponseEntity.ok(SuccessResponse.from(Map.of("valid", exists)));
}

@Operation(summary = "프로필(비밀번호/이름) 수정")
@Parameters({
@Parameter(name = "type", description = "수정할 프로필 타입", example = "name/password/", required = true),
@Parameter(name = "type", description = "수정할 프로필 타입", required = true),
@Parameter(name = "req", description = "수정할 프로필 정보")
})
@PutMapping("")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,6 @@ public interface MemberRepository extends ExtendedRepository<Member, Long> {
Optional<Member> findByPhone(String phone);
boolean existsByUidOrEmailOrPhone(String uid, String email, String phone);
boolean existsByPhone(String phone);
boolean existsByUid(String uid);
boolean existsByPhoneAndUid(String phone, String uid);
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ public enum AccountErrorCode implements StatusCode {

INVALID_NOTIFICATION_TYPE_ERROR(BAD_REQUEST, "유효하지 않은 알림 타입입니다."),

MISSMATCH_PHONE_AND_UID_ERROR(BAD_REQUEST, "등록된 전화번호와 일치하지 않는 유저입니다."),

/* 404 */
NOT_FOUND_MEMBER_ERROR(NOT_FOUND, "존재하지 않는 회원입니다."),
NOT_FOUND_PHONE_ERROR(NOT_FOUND, "존재하지 않는 전화번호입니다."),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,11 @@ public AccountProfileRes getProfile(Long userId) {
return AccountProfileRes.from(member);
}

@Transactional(readOnly = true)
public boolean existsUid(String uid) {
return memberSearchService.isExistByUid(uid);
}

@Transactional
public void updateProfile(Long userId, ProfilePatchReq req, MemberAttrType type) {
Member member = memberSearchService.findById(userId);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -145,12 +145,21 @@ public void checkCertificationForSearch(SmsReq req, String code, SmsPrefix prefi
}

private void validateForSms(SmsPrefix prefix, SmsReq req) {
if (prefix.equals(SmsPrefix.REGISTER) && memberSearchService.isExistByPhone(req.to())) {
boolean isExistPhone = memberSearchService.isExistByPhone(req.to());
if (prefix.equals(SmsPrefix.REGISTER) && isExistPhone) {
log.warn("중복된 전화번호로 인한 회원가입 요청 실패: {}", req.to());
throw new GlobalErrorException(AccountErrorCode.DUPLICATE_PHONE_ERROR);
} else if (!prefix.equals(SmsPrefix.REGISTER) && !memberSearchService.isExistByPhone(req.to())) {
} else if (prefix.equals(SmsPrefix.UID) && !isExistPhone) {
log.warn("DB에 존재하지 않는 전화번호로 인한 SMS 인증 요청 실패: {}", req.to());
throw new GlobalErrorException(AccountErrorCode.NOT_FOUND_PHONE_ERROR);
} else if (prefix.equals(SmsPrefix.PASSWORD)) {
if (!isExistPhone) {
log.warn("DB에 존재하지 않는 전화번호로 인한 SMS 인증 요청 실패: {}", req.to());
throw new GlobalErrorException(AccountErrorCode.NOT_FOUND_PHONE_ERROR);
} else if (!memberSearchService.isExistByPhoneAndUid(req.to(), req.uid())) {
log.warn("등록된 유저 전화번호와 다른 전화번호 입력: {}", req.to());
throw new GlobalErrorException(AccountErrorCode.MISSMATCH_PHONE_AND_UID_ERROR);
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,4 +41,14 @@ public boolean isExistByUidOrEmailOrPhone(String uid, String email, String phone
public boolean isExistByPhone(String phone) {
return memberRepository.existsByPhone(phone);
}

@Transactional(readOnly = true)
public boolean isExistByUid(String uid) {
return memberRepository.existsByUid(uid);
}

@Transactional(readOnly = true)
public boolean isExistByPhoneAndUid(String phone, String uid) {
return memberRepository.existsByPhoneAndUid(phone, uid);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@
@Getter
@RequiredArgsConstructor
public enum SmsPrefix {
REGISTER("register@"),
PASSWORD("password@"),
UID("uid@");
REGISTER("register"),
PASSWORD("password"),
UID("uid");

private final String prefix;

public String getTopic(String phoneNumber) {
return this.prefix + phoneNumber;
return this.prefix + '@' + phoneNumber;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,11 @@ private SuccessResponse(Map<String, T> data) {
*/
public static <T> SuccessResponse<T> from(T data) {
String key = getDtoName(data);

if (data instanceof Map) {
key = ((Map<?, ?>) data).keySet().stream().findFirst().orElse(null).toString();
data = (T) ((Map<?, ?>) data).get(key);
}
if (key == null) key = "data";

return SuccessResponse.<T>builder()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ public class JwtAuthenticationFilter extends OncePerRequestFilter {
"/api/v1/auth/refresh",
"/api/v1/auth/register-sms/**", "/api/v1/auth/search-sms/**",
"/api/v1/accounts/search", "/api/v1/accounts/search/**",
"/api/v1/accounts/exists", "/api/v1/accounts/exists/**",

"/v3/api-docs/**", "/swagger-ui/**", "/swagger",
"/favicon.ico"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
public record SmsReq(
@Schema(description = "수신번호")
@NotNull(message = "수신번호는 필수 입력값입니다.")
String to
String to,
@Schema(description = "아이디")
String uid
) {
}
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,10 @@ public class SecurityConfig {
// API
"/api/v1/auth/register", "/api/v1/auth/login", "/api/v1/auth/refresh",
"/api/v1/auth/register-sms/**", "/api/v1/auth/search-sms/**",
"/api/v1/accounts/search", "/api/v1/accounts/search/**"
"/api/v1/accounts/search", "/api/v1/accounts/search/**",
};
private static final String[] publicReadOnlyPublicEndpoints = {
"/api/v1/accounts/exists", "/api/v1/accounts/exists/**"
};

@Bean
Expand Down
Loading