diff --git a/src/main/java/org/prgrms/nabimarketbe/domain/card/entity/Card.java b/src/main/java/org/prgrms/nabimarketbe/domain/card/entity/Card.java index 68ef50d7..dbf4bd69 100644 --- a/src/main/java/org/prgrms/nabimarketbe/domain/card/entity/Card.java +++ b/src/main/java/org/prgrms/nabimarketbe/domain/card/entity/Card.java @@ -6,7 +6,6 @@ import lombok.NoArgsConstructor; import org.prgrms.nabimarketbe.global.BaseEntity; import org.prgrms.nabimarketbe.domain.item.entity.Item; -import org.prgrms.nabimarketbe.global.annotation.ValidEnum; import org.prgrms.nabimarketbe.global.error.BaseException; import org.prgrms.nabimarketbe.global.error.ErrorCode; diff --git a/src/main/java/org/prgrms/nabimarketbe/domain/item/entity/Item.java b/src/main/java/org/prgrms/nabimarketbe/domain/item/entity/Item.java index 357b66e8..de711d23 100644 --- a/src/main/java/org/prgrms/nabimarketbe/domain/item/entity/Item.java +++ b/src/main/java/org/prgrms/nabimarketbe/domain/item/entity/Item.java @@ -22,7 +22,6 @@ public class Item extends BaseEntity { @Column(name = "item_id", nullable = false) private Long itemId; - @NotBlank(message = "공백을 허용하지 않습니다.") @Column(name = "item_name", nullable = false) private String itemName; diff --git a/src/main/java/org/prgrms/nabimarketbe/domain/user/dto/request/UserSignInRequestDTO.java b/src/main/java/org/prgrms/nabimarketbe/domain/user/dto/request/UserSignInRequestDTO.java index a2a75a40..49f91ae8 100644 --- a/src/main/java/org/prgrms/nabimarketbe/domain/user/dto/request/UserSignInRequestDTO.java +++ b/src/main/java/org/prgrms/nabimarketbe/domain/user/dto/request/UserSignInRequestDTO.java @@ -19,5 +19,14 @@ public User toEntity() { .role(Role.USER.getKey()) .build(); } + + public User toEntity(String nickName) { + return User.builder() + .accountId(accountId) + .nickname(nickName) + .provider(provider) + .role(Role.USER.getKey()) + .build(); + } } diff --git a/src/main/java/org/prgrms/nabimarketbe/domain/user/service/SignService.java b/src/main/java/org/prgrms/nabimarketbe/domain/user/service/SignService.java index 94712b46..105d6e52 100644 --- a/src/main/java/org/prgrms/nabimarketbe/domain/user/service/SignService.java +++ b/src/main/java/org/prgrms/nabimarketbe/domain/user/service/SignService.java @@ -8,8 +8,6 @@ import org.prgrms.nabimarketbe.domain.user.repository.UserRepository; import org.prgrms.nabimarketbe.global.security.jwt.dto.TokenResponseDTO; import org.prgrms.nabimarketbe.global.security.jwt.provider.JwtProvider; -import org.prgrms.nabimarketbe.global.util.ResponseFactory; -import org.prgrms.nabimarketbe.global.util.model.CommonResult; import org.prgrms.nabimarketbe.oauth2.google.dto.GoogleUserInfoDTO; import org.prgrms.nabimarketbe.oauth2.kakao.dto.KakaoProfile; import org.springframework.stereotype.Service; @@ -31,30 +29,37 @@ public class SignService { private final RandomNicknameGenerator randomNicknameGenerator; @Transactional - public CommonResult signInBySocial(KakaoProfile kakaoProfile) { - CommonResult result = signIn(UserSignInRequestDTO.builder() + public UserLoginResponseDTO signInBySocial(KakaoProfile kakaoProfile) { + UserSignInRequestDTO userSignInRequestDTO = UserSignInRequestDTO.builder() .accountId(kakaoProfile.getId()) .nickname(kakaoProfile.getProperties().getNickname()) .provider("kakao") - .build()); + .build(); - return ResponseFactory.getSingleResult(result); + UserLoginResponseDTO result = signIn(userSignInRequestDTO); + + return result; } + @Transactional - public CommonResult signIn(UserSignInRequestDTO userSignInRequestDTO) { - Optional user = userRepository.findByAccountIdAndProvider( - userSignInRequestDTO.accountId(), - userSignInRequestDTO.provider() - ); - - if (user.isPresent()) { - return ResponseFactory.getSingleResult(jwtProvider.createTokenDTO( - user.get().getUserId(), user.get().getRole()) - ); - } - - User savedUser = userRepository.save(userSignInRequestDTO.toEntity()); - return ResponseFactory.getSingleResult(jwtProvider.createTokenDTO(savedUser.getUserId(), savedUser.getRole())); + public UserLoginResponseDTO signIn(UserSignInRequestDTO userSignInRequestDTO) { + String accountId = userSignInRequestDTO.accountId(); + + Optional optionalUser = userRepository.findByAccountId(accountId); + + User user = optionalUser.orElseGet(() -> { + try { + return signUp(userSignInRequestDTO); + } catch (JsonProcessingException e) { + throw new RuntimeException(e); + } + }); + + TokenDTO tokenDTO = jwtProvider.createTokenDTO(user.getUserId(), user.getRole()); + + UserLoginResponseDTO response = UserLoginResponseDTO.of(user, tokenDTO); + + return response; } @Transactional @@ -85,4 +90,13 @@ public User signUp(GoogleUserInfoDTO googleUserInfoDTO) throws JsonProcessingExc return savedUser; } + + @Transactional + public User signUp(UserSignInRequestDTO userSignInRequestDTO) throws JsonProcessingException { + String randomNickname = randomNicknameGenerator.generateRandomNickname(); + User user = userSignInRequestDTO.toEntity(randomNickname); + User savedUser = userRepository.save(user); + + return savedUser; + } } diff --git a/src/main/java/org/prgrms/nabimarketbe/global/config/WebConfig.java b/src/main/java/org/prgrms/nabimarketbe/global/config/WebConfig.java index c8344832..8075da3b 100644 --- a/src/main/java/org/prgrms/nabimarketbe/global/config/WebConfig.java +++ b/src/main/java/org/prgrms/nabimarketbe/global/config/WebConfig.java @@ -6,7 +6,6 @@ @Configuration public class WebConfig { - @Bean public RestTemplate restTemplate() { return new RestTemplate(); diff --git a/src/main/java/org/prgrms/nabimarketbe/oauth2/kakao/api/OAuth2Controller.java b/src/main/java/org/prgrms/nabimarketbe/oauth2/kakao/api/OAuth2Controller.java index 37163673..8976e09b 100644 --- a/src/main/java/org/prgrms/nabimarketbe/oauth2/kakao/api/OAuth2Controller.java +++ b/src/main/java/org/prgrms/nabimarketbe/oauth2/kakao/api/OAuth2Controller.java @@ -4,22 +4,17 @@ import javax.servlet.http.HttpServletResponse; +import org.prgrms.nabimarketbe.domain.user.dto.response.UserLoginResponseDTO; import org.prgrms.nabimarketbe.domain.user.service.SignService; import org.prgrms.nabimarketbe.global.util.ResponseFactory; import org.prgrms.nabimarketbe.global.util.model.CommonResult; +import org.prgrms.nabimarketbe.global.util.model.SingleResult; import org.prgrms.nabimarketbe.oauth2.kakao.dto.KakaoProfile; import org.prgrms.nabimarketbe.oauth2.kakao.service.OAuth2Service; import org.springframework.core.env.Environment; -import org.springframework.http.HttpEntity; -import org.springframework.http.HttpHeaders; -import org.springframework.http.HttpStatus; -import org.springframework.http.MediaType; -import org.springframework.http.ResponseEntity; +import org.springframework.http.*; import org.springframework.util.MultiValueMap; -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; +import org.springframework.web.bind.annotation.*; import org.springframework.web.client.RestTemplate; import lombok.RequiredArgsConstructor; @@ -45,11 +40,13 @@ public void socialLogin(HttpServletResponse response) throws IOException { } @GetMapping(value = "/redirect") - public CommonResult redirectKakao(@RequestParam String code) { + public ResponseEntity> redirectKakao(@RequestParam String code) { KakaoProfile profile = OAuth2Service.getResultProfile(code); if (profile == null) throw new RuntimeException("카카오에 해당 회원이 없습니다."); + UserLoginResponseDTO userLoginResponseDTO = signService.signInBySocial(profile); + SingleResult response = ResponseFactory.getSingleResult(userLoginResponseDTO); - return signService.signInBySocial(profile); + return ResponseEntity.ok(response); } //TODO : 사용자가 accessToken 넘기는건 아닌거 같음 diff --git a/src/main/java/org/prgrms/nabimarketbe/oauth2/kakao/service/OAuth2Service.java b/src/main/java/org/prgrms/nabimarketbe/oauth2/kakao/service/OAuth2Service.java index 04db56b5..93be90d3 100644 --- a/src/main/java/org/prgrms/nabimarketbe/oauth2/kakao/service/OAuth2Service.java +++ b/src/main/java/org/prgrms/nabimarketbe/oauth2/kakao/service/OAuth2Service.java @@ -1,8 +1,10 @@ package org.prgrms.nabimarketbe.oauth2.kakao.service; import com.google.gson.Gson; + import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; + import org.prgrms.nabimarketbe.oauth2.kakao.dto.KakaoProfile; import org.prgrms.nabimarketbe.oauth2.kakao.dto.RetKakaoOAuth; import org.springframework.beans.factory.annotation.Value; diff --git a/src/main/resources/application.yml b/src/main/resources/application.yml index ea593d3b..f08c04e6 100644 --- a/src/main/resources/application.yml +++ b/src/main/resources/application.yml @@ -2,7 +2,6 @@ spring: profiles: active: local - springdoc: version: '@springdoc.version@' swagger-ui: