Skip to content

Commit

Permalink
#15 refactor : 로그인 정책 변경으로 인한 로직 수정 및 리팩토링
Browse files Browse the repository at this point in the history
- 자체 회원가입 한 유저의 이메일이 깃허브 로그인 이메일과 같을 때 해당 유저의 정보를 깃허브 정보로 갱신 하려 했으나 로그인을 하면서 회원정보를 갱신하는 것은 로그인이 너무 많은 일을 하게 되는 것 같아서
깃허브로 로그인/회원가입 한 유저는 자체 로그인을 이용하지 못하게 변경
  • Loading branch information
DOEKYONG committed Aug 4, 2023
1 parent 822eef0 commit 2637aba
Showing 1 changed file with 14 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import codesquad.issueTracker.jwt.util.OauthProvider;
import codesquad.issueTracker.user.domain.LoginType;
import java.nio.charset.StandardCharsets;
import java.util.List;
import java.util.Map;
import java.util.Collections;

Expand Down Expand Up @@ -59,7 +60,7 @@ public LoginResponseDto login(LoginRequestDto loginRequestDto) {
User user = userRepository.findByEmail(loginRequestDto.getEmail())
.orElseThrow(() -> new CustomException(ErrorCode.NOT_FOUND_USER));
if(user.getPassword() == null){
throw new CustomException(ErrorCode.FAILED_LOGIN_USER);
throw new CustomException(ErrorCode.GITHUB_LOGIN_USER);
}
validateLoginUser(loginRequestDto, user);
validateLoginType(LoginType.LOCAL, user.getLoginType());
Expand Down Expand Up @@ -104,35 +105,17 @@ public void logout(HttpServletRequest request) {

public LoginResponseDto oauthLogin(String providerName, String code) {
OauthProvider provider = inMemoryProviderRepository.findByProviderName(providerName);

OauthTokenResponse tokenResponse = getToken(code, provider);

User user = getUserProfile(providerName, tokenResponse, provider).toUser(providerName);
User existUser = userRepository.findByEmail(user.getEmail()).orElse(null);
Long userId;
if(existUser != null){
if(!existUser.getLoginType().getType().equals(providerName) && existUser.getLoginType() != LoginType.LOCAL) {
throw new CustomException(ErrorCode.ALREADY_EXIST_USER);
} else if (existUser.getLoginType().getType().equals(providerName)) {
userId = existUser.getId();
// userRepository.deleteTokenByUserId(userId);
} else {
userId = userRepository.updateUserLoginType(existUser, user);
// userRepository.deleteTokenByUserId(userId);
}
} else {
userId = userRepository.insert(user);
}

Jwt jwt = jwtProvider.createJwt(Map.of("userId", userId));
userRepository.insertRefreshToken(userId, jwt.getRefreshToken());

User existUser =userRepository.findByEmail(user.getEmail()).orElseGet(() -> insertOauthUser(user));
validateLoginType(user.getLoginType(), existUser.getLoginType());
Jwt jwt = insertToken(existUser.getId());
return LoginResponseDto.builder()
.userId(userId)
.accessToken(jwt.getAccessToken())
.refreshToken(jwt.getRefreshToken())
.profileImgUrl(user.getProfileImg())
.build();
.userId(existUser.getId())
.accessToken(jwt.getAccessToken())
.refreshToken(jwt.getRefreshToken())
.profileImgUrl(user.getProfileImg())
.build();
}

private OauthTokenResponse getToken(String code, OauthProvider provider) {
Expand Down Expand Up @@ -204,4 +187,8 @@ private List<Map<String, Object>> getUserEmail(OauthTokenResponse tokenResponse)
.block();
}

private User insertOauthUser(User user){
userRepository.insert(user);
return userRepository.findByEmail(user.getEmail()).orElseThrow(() -> new CustomException(ErrorCode.NOT_FOUND_USER));
}
}

0 comments on commit 2637aba

Please sign in to comment.