Skip to content

Commit

Permalink
Merge pull request #127 from Mojacknong/fix_126/유저-생성
Browse files Browse the repository at this point in the history
Fix 126/유저 생성
  • Loading branch information
Ryeolee authored Sep 14, 2024
2 parents d4f8a38 + 70ad5bb commit 3b3c373
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 31 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -40,17 +40,11 @@ public class AuthService {

@Transactional
public BaseResponseDto<LoginResponseDto> googleLogin(String googleAccessToken) {

return BaseResponseDto.of(SuccessCode.SUCCESS,
googleLogin.loginMethod(googleAccessToken));
return BaseResponseDto.of(SuccessCode.SUCCESS, googleLogin.loginMethod(googleAccessToken));
}
@Transactional
public BaseResponseDto<LoginResponseDto> kakaoLogin(String kakaoAccessToken) {

return BaseResponseDto.of(SuccessCode.SUCCESS,
kakaoLogin.loginMethod(kakaoAccessToken));


return BaseResponseDto.of(SuccessCode.SUCCESS, kakaoLogin.loginMethod(kakaoAccessToken));
}
@Transactional
public BaseResponseDto<Void> logout(Long userId) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.modernfarmer.farmusspring.domain.auth.dto.LoginResponseDto;
import com.modernfarmer.farmusspring.domain.auth.repository.RedisManager;
import com.modernfarmer.farmusspring.domain.auth.util.social.dto.GoogleUserResponseDto;
import com.modernfarmer.farmusspring.domain.history.helper.HistoryHelper;
import com.modernfarmer.farmusspring.domain.user.repository.UserRepository;
import com.modernfarmer.farmusspring.global.common.security.JwtTokenProvider;
import com.modernfarmer.farmusspring.global.response.BaseResponseDto;
Expand All @@ -12,8 +13,8 @@
@Component
public class GoogleLogin extends SocialLogin{

public GoogleLogin(WebClient webClient, JwtTokenProvider jwtTokenProvider, RedisManager redisManager, UserRepository userRepository) {
super(webClient, jwtTokenProvider, redisManager, userRepository);
public GoogleLogin(WebClient webClient, JwtTokenProvider jwtTokenProvider, RedisManager redisManager, UserRepository userRepository, HistoryHelper historyHelper) {
super(webClient, jwtTokenProvider, redisManager, userRepository, historyHelper);
}

@Override
Expand All @@ -22,8 +23,6 @@ public LoginResponseDto loginMethod(String socialToken) {
socialToken,
"https://www.googleapis.com/oauth2/v2/userinfo",
GoogleUserResponseDto.class);


return login(socialUserData);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import com.modernfarmer.farmusspring.domain.auth.dto.LoginResponseDto;
import com.modernfarmer.farmusspring.domain.auth.repository.RedisManager;
import com.modernfarmer.farmusspring.domain.auth.util.social.dto.KakaoUserResponseDto;
import com.modernfarmer.farmusspring.domain.history.helper.HistoryHelper;
import com.modernfarmer.farmusspring.domain.user.repository.UserRepository;
import com.modernfarmer.farmusspring.global.common.security.JwtTokenProvider;
import com.modernfarmer.farmusspring.global.response.BaseResponseDto;
Expand All @@ -13,8 +14,8 @@
@Component
public class KakaoLogin extends SocialLogin {

public KakaoLogin(WebClient webClient, JwtTokenProvider jwtTokenProvider, RedisManager redisManager, UserRepository userRepository) {
super(webClient, jwtTokenProvider, redisManager, userRepository);
public KakaoLogin(WebClient webClient, JwtTokenProvider jwtTokenProvider, RedisManager redisManager, UserRepository userRepository, HistoryHelper historyHelper) {
super(webClient, jwtTokenProvider, redisManager, userRepository, historyHelper);
}

@Override
Expand All @@ -23,8 +24,6 @@ public LoginResponseDto loginMethod(String socialToken) {
socialToken,
"https://kapi.kakao.com/v2/user/me",
KakaoUserResponseDto.class);


return login(socialUserData);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.modernfarmer.farmusspring.domain.auth.dto.LoginResponseDto;
import com.modernfarmer.farmusspring.domain.auth.repository.RedisManager;
import com.modernfarmer.farmusspring.domain.auth.util.social.dto.SocialUserResponseDto;
import com.modernfarmer.farmusspring.domain.history.helper.HistoryHelper;
import com.modernfarmer.farmusspring.domain.user.entity.User;
import com.modernfarmer.farmusspring.domain.user.exception.custom.UserNotFoundException;
import com.modernfarmer.farmusspring.domain.user.repository.UserRepository;
Expand All @@ -28,30 +29,24 @@ abstract public class SocialLogin {
private final JwtTokenProvider jwtTokenProvider;
private final RedisManager redisManager;
private final UserRepository userRepository;
private final HistoryHelper historyHelper;


public abstract LoginResponseDto loginMethod(String socialToken);

public LoginResponseDto login(SocialUserResponseDto socialUserData){

verifySocialUserData(socialUserData);

userRepository.findByUserNumber(String.valueOf(socialUserData.getId()))
.orElseGet(() -> {

socialSignUp(socialUserData);
return null;
});

Optional<User> userLoginData = Optional.ofNullable(userRepository.findByUserNumber(String.valueOf(socialUserData.getId())).orElseThrow(() -> new UserNotFoundException("해당 유저의 정보가 존재하지 않습니다.", UserErrorCode.NOT_FOUND_USER)));

String refreshToken = jwtTokenProvider.createRefreshToken(userLoginData.get().getId());
String accessToken = jwtTokenProvider.createAccessToken(
userLoginData.get().getId(),
String.valueOf(userLoginData.get().getRole()));

String accessToken = jwtTokenProvider.createAccessToken(userLoginData.get().getId(), String.valueOf(userLoginData.get().getRole()));
redisManager.setValueByKey(String.valueOf(userLoginData.get().getId()), refreshToken);

historyHelper.createUserHistory(userLoginData.get().getId());
return LoginResponseDto.of(
accessToken,
refreshToken,
Expand All @@ -60,33 +55,27 @@ public LoginResponseDto login(SocialUserResponseDto socialUserData){
}

private <T> void verifySocialUserData(T data){

if(data == null){

BaseResponseDto.of(UserErrorCode.NOT_FOUND_USER, "요청한 소셜 유저 정보가 없습니다.");
}
}

private <T extends SocialUserResponseDto> void socialSignUp(T socialUserData) {

User user = User.createUser(
"USER",
String.valueOf(socialUserData.getId()),
true
);
userRepository.save(user);

}

public <T> T getUserData(String accessToken, String apiUrl, Class<T> responseType){

Mono<T> userInfoMono = webClient
.get()
.uri(apiUrl)
.headers(headers -> headers.set("Authorization", accessToken))
.retrieve()
.bodyToMono(responseType);

return userInfoMono.block();
}

Expand Down

0 comments on commit 3b3c373

Please sign in to comment.