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

✨ 닉네임 중복검사 API #31

Merged
merged 8 commits into from
Apr 1, 2024
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/**"};
asn6878 marked this conversation as resolved.
Show resolved Hide resolved
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);
}
}
Loading