Skip to content

Commit

Permalink
NO-JIRA-refactor : 카카오 - user 정보 내려주는 로직 추가
Browse files Browse the repository at this point in the history
  • Loading branch information
born-A committed Nov 7, 2023
1 parent 956ddcb commit c032890
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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> 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<User> 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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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("카카오에 해당 회원이 없습니다.");

Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand Down

0 comments on commit c032890

Please sign in to comment.