Skip to content

Commit

Permalink
feat: OAuth2ProviderService 구현
Browse files Browse the repository at this point in the history
  • Loading branch information
yeonjy committed Sep 27, 2024
1 parent 26070bd commit e3b15eb
Showing 1 changed file with 59 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package com.pyro.yolog.global.oauth2.service;

import com.pyro.yolog.global.oauth2.dto.LoginRequest;
import com.pyro.yolog.global.oauth2.userInfo.GoogleOAuth2UserInfo;
import com.pyro.yolog.global.oauth2.userInfo.KakaoOAuth2UserInfo;
import com.pyro.yolog.global.oauth2.userInfo.OAuth2UserInfo;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Service;
import org.springframework.web.reactive.function.client.WebClient;

import java.util.Map;

import static com.pyro.yolog.domain.member.entity.SocialType.GOOGLE;
import static com.pyro.yolog.domain.member.entity.SocialType.KAKAO;

@Service
@RequiredArgsConstructor
public class OAuth2ProviderService {
public OAuth2UserInfo getUserInfo(LoginRequest request) {
return switch (request.getSocialType()) {
case GOOGLE -> getKakaoUserInfo(request);
case KAKAO -> getKakaoUserInfo(request);
};
}

private OAuth2UserInfo getKakaoUserInfo(LoginRequest request) {
Map attributes = WebClient.create(KAKAO.getProviderUrl())
.get()
.headers(httpHeaders -> {
httpHeaders.add(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE);
httpHeaders.setBearerAuth(request.getToken());
})
.accept(MediaType.APPLICATION_JSON)
.retrieve()
.bodyToMono(Map.class)
.log()
.block();

return new KakaoOAuth2UserInfo(attributes);
}

private OAuth2UserInfo getGoogleUserInfo(LoginRequest request) {
Map attributes = WebClient.create(GOOGLE.getProviderUrl())
.get()
.headers(httpHeaders -> {
httpHeaders.add(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE);
httpHeaders.setBearerAuth(request.getToken());
})
.accept(MediaType.APPLICATION_JSON)
.retrieve()
.bodyToMono(Map.class)
.log()
.block();

return new GoogleOAuth2UserInfo(attributes);
}
}

0 comments on commit e3b15eb

Please sign in to comment.