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 4480362b..78cd2856 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.TokenDTO; 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,33 @@ 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(() -> { + return userRepository.save(userSignInRequestDTO.toEntity()); + }); + + TokenDTO tokenDTO = jwtProvider.createTokenDTO(user.getUserId(), user.getRole()); + + UserLoginResponseDTO response = UserLoginResponseDTO.of(user, tokenDTO); + + return response; } @Transactional 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..58df02f3 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,16 @@ 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.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,7 +39,7 @@ public void socialLogin(HttpServletResponse response) throws IOException { } @GetMapping(value = "/redirect") - public CommonResult redirectKakao(@RequestParam String code) { + public UserLoginResponseDTO redirectKakao(@RequestParam String code) { KakaoProfile profile = OAuth2Service.getResultProfile(code); if (profile == null) throw new RuntimeException("카카오에 해당 회원이 없습니다."); 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;