Skip to content

Commit

Permalink
[NABI-101]--refactor : 로그인 로직 변경
Browse files Browse the repository at this point in the history
  • Loading branch information
BeommoKoo-dev committed Nov 1, 2023
1 parent a6b0949 commit 85728a3
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -26,7 +27,7 @@
@Service
@RequiredArgsConstructor
public class SignService {
private final UserRepository userJpaRepo;
private final UserRepository userRepository;

private final OAuth2Service oAuth2Service;

Expand All @@ -52,7 +53,7 @@ public CommonResult signupBySocial(String accessToken) {

@Transactional
public CommonResult socialSignup(UserSignupRequestDto userSignupRequestDto) {
Optional<User> user = userJpaRepo.findByNicknameAndProvider(
Optional<User> user = userRepository.findByNicknameAndProvider(
userSignupRequestDto.nickname(),
userSignupRequestDto.provider()
);
Expand All @@ -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()));
}

Expand All @@ -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())
Expand All @@ -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<User> 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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -25,6 +27,8 @@
public class GoogleOAuth2Controller {
private final GoogleOAuth2Service oauthService;

private final SignService signService;

@GetMapping("/login")
public void socialLogin(
HttpServletResponse response
Expand All @@ -38,7 +42,10 @@ public void socialLogin(
public ResponseEntity<MyCommonResult<UserLoginResponseDTO>> callback(
@RequestParam(name = "code") String code
) throws JsonProcessingException {
UserLoginResponseDTO loginResponseDTO = oauthService.OAuth2Login(code);
GoogleUserInfoDTO googleUserInfoDTO = oauthService.OAuth2Login(code);

UserLoginResponseDTO loginResponseDTO = signService.signIn(googleUserInfoDTO);

MyCommonResult<UserLoginResponseDTO> response = new MyCommonResult<>(
"C001",
"성공했습니다.",
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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<User> 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 {
Expand Down

0 comments on commit 85728a3

Please sign in to comment.