Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Nabi 41 yejin refactor kakao login user #21

Merged
merged 9 commits into from
Nov 7, 2023
Original file line number Diff line number Diff line change
Expand Up @@ -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;

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

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

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.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;
Expand All @@ -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> 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(() -> {
try {
return signUp(userSignInRequestDTO);
} catch (JsonProcessingException e) {
throw new RuntimeException(e);
}
});

TokenDTO tokenDTO = jwtProvider.createTokenDTO(user.getUserId(), user.getRole());
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

p1
TokenDTO클래스는 네이밍이 애매해서, 제가 TokenResponseDTO로 고친 이후 dev에 머지해두었습니다.
PR 날리기 전에 dev브랜치의 커밋을 머지하시는 것이 좋을 것 같아요! (해당 PR머지시 기존 파일들이 덮어씌워짐)

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.


UserLoginResponseDTO response = UserLoginResponseDTO.of(user, tokenDTO);

return response;
}

@Transactional
Expand Down Expand Up @@ -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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@

@Configuration
public class WebConfig {

@Bean
public RestTemplate restTemplate() {
return new RestTemplate();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -45,11 +40,13 @@ public void socialLogin(HttpServletResponse response) throws IOException {
}

@GetMapping(value = "/redirect")
public CommonResult redirectKakao(@RequestParam String code) {
public ResponseEntity<SingleResult<UserLoginResponseDTO>> redirectKakao(@RequestParam String code) {
KakaoProfile profile = OAuth2Service.getResultProfile(code);
if (profile == null) throw new RuntimeException("카카오에 해당 회원이 없습니다.");
UserLoginResponseDTO userLoginResponseDTO = signService.signInBySocial(profile);
SingleResult<UserLoginResponseDTO> response = ResponseFactory.getSingleResult(userLoginResponseDTO);

return signService.signInBySocial(profile);
return ResponseEntity.ok(response);
}

//TODO : 사용자가 accessToken 넘기는건 아닌거 같음
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
1 change: 0 additions & 1 deletion src/main/resources/application.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ spring:
profiles:
active: local


springdoc:
version: '@springdoc.version@'
swagger-ui:
Expand Down
Loading