Skip to content

Commit

Permalink
refactor: 인증처리 Service method 세분화 (#35)
Browse files Browse the repository at this point in the history
  • Loading branch information
mybloom committed Jun 11, 2022
1 parent e9a6796 commit 0f87713
Showing 1 changed file with 48 additions and 23 deletions.
71 changes: 48 additions & 23 deletions BE/src/main/java/org/team4/airbnb/auth/OAuthService.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import org.springframework.core.ParameterizedTypeReference;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.reactive.function.client.WebClient;
Expand All @@ -27,38 +28,63 @@ public class OAuthService {
private final CustomerRepository customerRepository;
private final JwtTokenProvider jwtTokenProvider;

private final String TOKEN_TYPE = "Bearer";

@Transactional
public LoginResponse processLogin(String provider, String authCode) {
OauthProvider oauthProvider = memoryProviderRepository.findByProviderName(provider);

//1. access token 얻어오기
OauthTokenResponse tokenResponse = getAccessTokenFromOauth(authCode, oauthProvider);
String userId = processOauth(provider, authCode, oauthProvider);
checkIsAndSignUpMember(userId);
LoginResponse response = createJwtTokenAndMakeResponse(userId);

//2. 유저 정보 얻어오기
UserProfile userProfile = getUserInfoFromOauth(provider, oauthProvider,
tokenResponse);
String userIdViaUserProfile = userProfile.getUserId();
return response;
}

if (userIdViaUserProfile == null) {
new GithubInfoNotFoundException();
}
/**
* JWT token을 생성하고, 로그인 응답 DTO 객체를 생성한다.
* @param userId
* @return 로그인 응답 DTO
*/
private LoginResponse createJwtTokenAndMakeResponse(String userId) {
String accessToken = jwtTokenProvider.createAccessToken(userId);
String refreshToken = jwtTokenProvider.createRefreshToken();

//3. DB에 유저 정보 저장 (최초 로그인 시 1번)
Customer customer = Customer.of(userIdViaUserProfile);
//기존 db에 사용자 있는지 확인 후, 없다면 DB에 유저 정보 저장 후 로그인처리
Customer findCustomer = customerRepository.findByUserId(userIdViaUserProfile)
.orElse(null);
return LoginResponse.of(accessToken, refreshToken, TOKEN_TYPE);
}

if (findCustomer == null) {
customer = customerRepository.save(customer);
}
/**
* 회원 DB에 해당 회원정보 없을 경우, 가입 처리 가입 처리는, 최초 로그인 시 1번 수행된다.
*
* @param userId
*/
private void checkIsAndSignUpMember(String userId) {
customerRepository.findByUserId(userId)
.ifPresentOrElse(customer -> {},
() -> customerRepository.save(Customer.of(userId)));
}

//4. JWT 토큰 생성 후 응답 DTO 전달
String accessToken = jwtTokenProvider.createAccessToken(userIdViaUserProfile);
String refreshToken = jwtTokenProvider.createRefreshToken();
/**
* github 인증서버와 인증 처리를 하고, 사용자 정보를 가져온다. 서드파티에서 제공하는 사용자 정보중, userId 정보만 사용하기 때문에 userId를 반환한다.
*
* @param provider
* @param authCode
* @param oauthProvider
* @return userId
*/
private String processOauth(String provider, String authCode, OauthProvider oauthProvider) {
//1. OAuth access token 얻어오기
OauthTokenResponse tokenResponse = getAccessTokenFromOauth(authCode, oauthProvider);

LoginResponse loginResponse = LoginResponse.of(accessToken, refreshToken, "Bearer");
//2. OAuth 유저 정보 얻어오기
UserProfile userProfile = getUserInfoFromOauth(provider, oauthProvider,
tokenResponse);
String userId = userProfile.getUserId();

return loginResponse;
if (userId == null) {
new GithubInfoNotFoundException();
}
return userId;
}

private OauthTokenResponse getAccessTokenFromOauth(String authCode,
Expand Down Expand Up @@ -93,7 +119,6 @@ private MultiValueMap<String, String> makeBodyForTokenRequest(String authCode,
return formData;
}


private UserProfile getUserInfoFromOauth(String provider, OauthProvider oauthProvider,
OauthTokenResponse tokenResponse) {
Map<String, Object> userInfo = requestToOauthServer(oauthProvider, tokenResponse);
Expand Down

0 comments on commit 0f87713

Please sign in to comment.