Skip to content

Commit

Permalink
[feat] 닉네임 중복 확인 api 구현 #10
Browse files Browse the repository at this point in the history
  • Loading branch information
suhhyun524 committed May 12, 2023
1 parent 534fc8c commit 95e3c1c
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Excepti
.and()
.authorizeHttpRequests((req) ->
req
.requestMatchers("/user/authenticate", "/user/signup", "/user/signin", "/user/test/**").permitAll()
.requestMatchers("/user/authenticate", "/user/**").permitAll()
.requestMatchers("/oauth/**").permitAll()
.requestMatchers("/source/url").permitAll()
.anyRequest().authenticated()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,23 +1,19 @@
package com.zonebug.debugging.controller;

import com.zonebug.debugging.domain.user.User;
import com.zonebug.debugging.dto.InfoUpdateDTO;
import com.zonebug.debugging.dto.LoginDto;
import com.zonebug.debugging.dto.TokenDto;
import com.zonebug.debugging.dto.UserDto;
import com.zonebug.debugging.security.jwt.JwtFilter;
import com.zonebug.debugging.dto.response.NicknameResponseDTO;
import com.zonebug.debugging.security.jwt.TokenProvider;
import com.zonebug.debugging.security.user.CustomUserDetails;
import com.zonebug.debugging.service.UserService;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.web.bind.annotation.*;

import java.util.Optional;
Expand All @@ -42,6 +38,13 @@ public ResponseEntity<TokenDto> signIn(@Valid @RequestBody LoginDto loginDto) {
return ResponseEntity.ok(userService.signIn(loginDto));
}

@GetMapping("/nickname")
public ResponseEntity<NicknameResponseDTO> checkNickname(
@RequestParam String nickname
){
return ResponseEntity.ok(userService.checkNickname(nickname));
}

@PostMapping("/authenticate")
public ResponseEntity<TokenDto> authorize(@Valid @RequestBody LoginDto loginDto) {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,6 @@ public interface UserRepository extends JpaRepository<User, Long> {

Optional<User> findByEmail(String email);
Optional<User> findById(Long userId);
Optional<User> findByNickname(String nickname);

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.zonebug.debugging.dto.response;

import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Getter;

@Getter
@Builder
@AllArgsConstructor
public class NicknameResponseDTO {

private boolean isPresent;
}
9 changes: 7 additions & 2 deletions src/main/java/com/zonebug/debugging/service/UserService.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,10 @@

import com.zonebug.debugging.domain.user.User;
import com.zonebug.debugging.domain.user.UserRepository;
import com.zonebug.debugging.dto.InfoUpdateDTO;
import com.zonebug.debugging.dto.LoginDto;
import com.zonebug.debugging.dto.TokenDto;
import com.zonebug.debugging.dto.UserDto;
import com.zonebug.debugging.security.user.CustomUserDetails;
import com.zonebug.debugging.dto.response.NicknameResponseDTO;
import com.zonebug.debugging.security.util.SecurityUtil;
import com.zonebug.debugging.security.jwt.TokenProvider;
import lombok.RequiredArgsConstructor;
Expand Down Expand Up @@ -54,6 +53,12 @@ public TokenDto signIn(LoginDto loginDto) {
return tokenDto;
}

public NicknameResponseDTO checkNickname(String nickname) {
Optional<User> findUser = userRepository.findByNickname(nickname);

return new NicknameResponseDTO(findUser.isPresent());

}

@Transactional(readOnly = true)
public Optional<User> getCurrentUser() {
Expand Down

0 comments on commit 95e3c1c

Please sign in to comment.