From 85728a3741612e31eb24fc2998611ba89acdcb45 Mon Sep 17 00:00:00 2001 From: BeommoKoo-dev <95630007+BeommoKoo-dev@users.noreply.github.com> Date: Thu, 2 Nov 2023 01:33:15 +0900 Subject: [PATCH] =?UTF-8?q?[NABI-101]--refactor=20:=20=EB=A1=9C=EA=B7=B8?= =?UTF-8?q?=EC=9D=B8=20=EB=A1=9C=EC=A7=81=20=EB=B3=80=EA=B2=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../domain/user/service/SignService.java | 30 +++++++++++++------ .../google/api/GoogleOAuth2Controller.java | 9 +++++- .../google/service/GoogleOAuth2Service.java | 17 ++--------- 3 files changed, 31 insertions(+), 25 deletions(-) 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 32d24388..2c5fb68e 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 @@ -2,6 +2,7 @@ import java.util.Optional; +import org.prgrms.nabimarketbe.domain.user.dto.UserLoginResponseDTO; import org.prgrms.nabimarketbe.domain.user.dto.sign.UserSignupRequestDto; import org.prgrms.nabimarketbe.domain.user.entity.User; import org.prgrms.nabimarketbe.domain.user.repository.UserRepository; @@ -26,7 +27,7 @@ @Service @RequiredArgsConstructor public class SignService { - private final UserRepository userJpaRepo; + private final UserRepository userRepository; private final OAuth2Service oAuth2Service; @@ -52,7 +53,7 @@ public CommonResult signupBySocial(String accessToken) { @Transactional public CommonResult socialSignup(UserSignupRequestDto userSignupRequestDto) { - Optional user = userJpaRepo.findByNicknameAndProvider( + Optional user = userRepository.findByNicknameAndProvider( userSignupRequestDto.nickname(), userSignupRequestDto.provider() ); @@ -61,7 +62,7 @@ public CommonResult socialSignup(UserSignupRequestDto userSignupRequestDto) { return responseFactory.getSingleResult(jwtProvider.createTokenDto(user.get().getUserId(), user.get().getRoles())); } - userJpaRepo.save(userSignupRequestDto.toEntity()); + userRepository.save(userSignupRequestDto.toEntity()); return responseFactory.getSingleResult(jwtProvider.createTokenDto(user.get().getUserId(), user.get().getRoles())); } @@ -77,7 +78,7 @@ public TokenResponseDTO reissue(TokenRequestDto tokenRequestDto) { Authentication authentication = jwtProvider.getAuthentication(accessToken); // user pk로 유저 검색 / repo 에 저장된 Refresh Token 이 없음 - User user = userJpaRepo.findById(Long.parseLong(authentication.getName())) + User user = userRepository.findById(Long.parseLong(authentication.getName())) .orElseThrow(() ->new RuntimeException("RefreshTokenException")); RefreshToken refreshToken = tokenJpaRepo.findByUserId(user.getUserId()) @@ -97,12 +98,23 @@ public TokenResponseDTO reissue(TokenRequestDto tokenRequestDto) { } @Transactional - public User signUp(GoogleUserInfoDTO googleUserInfoDTO) { - User user = googleUserInfoDTO.toEntity("randomNickName"); - User savedUser = userJpaRepo.save(user); + public UserLoginResponseDTO signIn(GoogleUserInfoDTO userInfo) { + String nameAttributeKey = userInfo.id(); - // TODO : 닉네임 랜덤 생성 - log.info("modified date : {}", savedUser.getModifiedDate()); + Optional optionalUser = userRepository.findByNameAttributeKey(nameAttributeKey); + + User user = optionalUser.orElseGet(() -> signUp(userInfo)); + TokenResponseDTO tokenResponseDTO = jwtProvider.createTokenDto(user.getUserId(), user.getRoles()); + + UserLoginResponseDTO response = UserLoginResponseDTO.from(user, tokenResponseDTO); + + return response; + } + + @Transactional + public User signUp(GoogleUserInfoDTO userInfo) { + User user = userInfo.toEntity("randomNickName"); + User savedUser = userRepository.save(user); return savedUser; } diff --git a/src/main/java/org/prgrms/nabimarketbe/oauth2/google/api/GoogleOAuth2Controller.java b/src/main/java/org/prgrms/nabimarketbe/oauth2/google/api/GoogleOAuth2Controller.java index 76b9c7d4..b52a9b03 100644 --- a/src/main/java/org/prgrms/nabimarketbe/oauth2/google/api/GoogleOAuth2Controller.java +++ b/src/main/java/org/prgrms/nabimarketbe/oauth2/google/api/GoogleOAuth2Controller.java @@ -5,7 +5,9 @@ import javax.servlet.http.HttpServletResponse; import org.prgrms.nabimarketbe.domain.user.dto.UserLoginResponseDTO; +import org.prgrms.nabimarketbe.domain.user.service.SignService; import org.prgrms.nabimarketbe.global.util.model.MyCommonResult; +import org.prgrms.nabimarketbe.oauth2.google.dto.GoogleUserInfoDTO; import org.prgrms.nabimarketbe.oauth2.google.service.GoogleOAuth2Service; import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.GetMapping; @@ -25,6 +27,8 @@ public class GoogleOAuth2Controller { private final GoogleOAuth2Service oauthService; + private final SignService signService; + @GetMapping("/login") public void socialLogin( HttpServletResponse response @@ -38,7 +42,10 @@ public void socialLogin( public ResponseEntity> callback( @RequestParam(name = "code") String code ) throws JsonProcessingException { - UserLoginResponseDTO loginResponseDTO = oauthService.OAuth2Login(code); + GoogleUserInfoDTO googleUserInfoDTO = oauthService.OAuth2Login(code); + + UserLoginResponseDTO loginResponseDTO = signService.signIn(googleUserInfoDTO); + MyCommonResult response = new MyCommonResult<>( "C001", "성공했습니다.", diff --git a/src/main/java/org/prgrms/nabimarketbe/oauth2/google/service/GoogleOAuth2Service.java b/src/main/java/org/prgrms/nabimarketbe/oauth2/google/service/GoogleOAuth2Service.java index ccd52b95..cd3e4125 100644 --- a/src/main/java/org/prgrms/nabimarketbe/oauth2/google/service/GoogleOAuth2Service.java +++ b/src/main/java/org/prgrms/nabimarketbe/oauth2/google/service/GoogleOAuth2Service.java @@ -1,12 +1,7 @@ package org.prgrms.nabimarketbe.oauth2.google.service; -import java.util.Optional; - -import org.prgrms.nabimarketbe.domain.user.dto.UserLoginResponseDTO; -import org.prgrms.nabimarketbe.domain.user.entity.User; import org.prgrms.nabimarketbe.domain.user.repository.UserRepository; import org.prgrms.nabimarketbe.domain.user.service.SignService; -import org.prgrms.nabimarketbe.global.security.jwt.dto.TokenResponseDTO; import org.prgrms.nabimarketbe.global.security.jwt.provider.JwtProvider; import org.prgrms.nabimarketbe.oauth2.google.domain.OAuth2; import org.prgrms.nabimarketbe.oauth2.google.dto.GoogleOAuth2TokenDTO; @@ -37,18 +32,10 @@ public String requestRedirectUrl() { return redirectUrl; } - public UserLoginResponseDTO OAuth2Login(String code) throws JsonProcessingException { + public GoogleUserInfoDTO OAuth2Login(String code) throws JsonProcessingException { GoogleUserInfoDTO googleUserInfoDTO = getUserInfoFromPlatform(code); - String nameAttributeKey = googleUserInfoDTO.id(); - - Optional optionalUser = userRepository.findByNameAttributeKey(nameAttributeKey); - - User user = optionalUser.orElseGet(() -> signService.signUp(googleUserInfoDTO)); - TokenResponseDTO tokenResponseDTO = jwtProvider.createTokenDto(user.getUserId(), user.getRoles()); - - UserLoginResponseDTO response = UserLoginResponseDTO.from(user, tokenResponseDTO); - return response; + return googleUserInfoDTO; } private GoogleUserInfoDTO getUserInfoFromPlatform(String code) throws JsonProcessingException {