Skip to content

Commit

Permalink
✨ 닉네임 중복검사 API (#31)
Browse files Browse the repository at this point in the history
* rename: auth controller '일반 회원 가입' 전화번호 인증 swagger 문서 상 명시

* feat: 닉네임 중복 검사 domain service 메서드 추가

* feat: username 중복 검사 api 개방

* fix: 중복 검사 체크 url을 anonymous endpoints에 추가

* fix: swagger endpoints와 read only public endpoints 분리

* fix: 닉네임 중복 검사 인가 기준 permit-all로 변경

* rename: is-exist-nickname -> is-exist-username

* rename: auth check controller 매개변수명 username으로 수정
  • Loading branch information
psychology50 authored Apr 1, 2024
1 parent f925160 commit b6943e4
Show file tree
Hide file tree
Showing 6 changed files with 63 additions and 8 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package kr.co.pennyway.api.apis.auth.controller;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import kr.co.pennyway.api.apis.auth.usecase.AuthCheckUseCase;
import kr.co.pennyway.api.common.response.SuccessResponse;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@Slf4j
@Tag(name = "[계정 검사 API]")
@RestController
@RequiredArgsConstructor
@RequestMapping("/v1/duplicate")
public class AuthCheckController {
private final AuthCheckUseCase authCheckUseCase;

@Operation(summary = "닉네임 중복 검사")
@GetMapping("/username")
@PreAuthorize("permitAll()")
public ResponseEntity<?> checkUsername(@RequestParam @Validated String username) {
return ResponseEntity.ok(SuccessResponse.from("isDuplicate", authCheckUseCase.checkUsernameDuplicate(username)));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,14 @@ public class AuthController {
private final AuthUseCase authUseCase;
private final CookieUtil cookieUtil;

@Operation(summary = "인증번호 전송")
@Operation(summary = "일반 회원가입 인증번호 전송")
@PostMapping("/phone")
@PreAuthorize("isAnonymous()")
public ResponseEntity<?> sendCode(@RequestBody @Validated PhoneVerificationDto.PushCodeReq request) {
return ResponseEntity.ok(SuccessResponse.from("sms", authUseCase.sendCode(request)));
}

@Operation(summary = "인증번호 검증")
@Operation(summary = "일반 회원가입 인증번호 검증")
@PostMapping("/phone/verification")
@PreAuthorize("isAnonymous()")
public ResponseEntity<?> verifyCode(@RequestBody @Validated PhoneVerificationDto.VerifyCodeReq request) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package kr.co.pennyway.api.apis.auth.usecase;

import kr.co.pennyway.common.annotation.UseCase;
import kr.co.pennyway.domain.domains.user.service.UserService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.transaction.annotation.Transactional;

@Slf4j
@UseCase
@RequiredArgsConstructor
public class AuthCheckUseCase {
private final UserService userService;

@Transactional(readOnly = true)
public boolean checkUsernameDuplicate(String username) {
return userService.isExistUsername(username);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,9 @@
@ConditionalOnDefaultWebSecurity
@RequiredArgsConstructor
public class SecurityConfig {
private static final String[] READ_ONLY_PUBLIC_ENDPOINTS = {
"/favicon.ico",
// Swagger
"/api-docs/**", "/v3/api-docs/**", "/swagger-ui/**", "/swagger",
};
private static final String[] READ_ONLY_PUBLIC_ENDPOINTS = {"/favicon.ico", "/v1/duplicate/**"};
private static final String[] ANONYMOUS_ENDPOINTS = {"/v1/auth/**"};
private static final String[] SWAGGER_ENDPOINTS = {"/api-docs/**", "/v3/api-docs/**", "/swagger-ui/**", "/swagger",};

private final SecurityAdapterConfig securityAdapterConfig;
private final CorsConfigurationSource corsConfigurationSource;
Expand All @@ -46,7 +43,7 @@ public SecurityFilterChain filterChainDev(HttpSecurity http) throws Exception {
.cors((cors) -> cors.configurationSource(corsConfigurationSource))
.authorizeHttpRequests(
auth -> defaultAuthorizeHttpRequests(auth)
.requestMatchers(READ_ONLY_PUBLIC_ENDPOINTS).permitAll()
.requestMatchers(SWAGGER_ENDPOINTS).permitAll()
.anyRequest().authenticated()
).build();
}
Expand Down Expand Up @@ -81,6 +78,7 @@ private AbstractRequestMatcherRegistry<AuthorizeHttpRequestsConfigurer<HttpSecur
AuthorizeHttpRequestsConfigurer<HttpSecurity>.AuthorizationManagerRequestMatcherRegistry auth) {
return auth.requestMatchers(PathRequest.toStaticResources().atCommonLocations()).permitAll()
.requestMatchers(HttpMethod.OPTIONS, "*").permitAll()
.requestMatchers(HttpMethod.GET, READ_ONLY_PUBLIC_ENDPOINTS).permitAll()
.requestMatchers(ANONYMOUS_ENDPOINTS).anonymous();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,6 @@ public interface UserRepository extends JpaRepository<User, Long> {
Optional<User> findByPhone(String phone);

Optional<User> findByUsername(String username);

boolean existsByUsername(String username);
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,9 @@ public Optional<User> readUserByUsername(String username) {
public boolean isExistUser(Long id) {
return userRepository.existsById(id);
}

@Transactional(readOnly = true)
public boolean isExistUsername(String username) {
return userRepository.existsByUsername(username);
}
}

0 comments on commit b6943e4

Please sign in to comment.