From 76c227c9ffa596fda3f6f080e4959b6e9a369415 Mon Sep 17 00:00:00 2001 From: mybloom Date: Thu, 9 Jun 2022 15:54:14 +0900 Subject: [PATCH] =?UTF-8?q?feat:=20=EC=9D=B8=EC=A6=9D=EC=B2=98=EB=A6=AC=20?= =?UTF-8?q?=EB=A1=9C=EC=A7=81=EC=97=90=20db=EC=97=90=20=ED=9A=8C=EC=9B=90?= =?UTF-8?q?=EC=A0=95=EB=B3=B4=EA=B0=80=20=EC=97=86=EC=9D=84=20=EA=B2=BD?= =?UTF-8?q?=EC=9A=B0=20db=EC=97=90=20insert=EB=A1=9C=20=EA=B0=80=EC=9E=85?= =?UTF-8?q?=EC=B2=98=EB=A6=AC=20=20(ios-h/airbnb#35)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../org/team4/airbnb/auth/OAuthService.java | 24 ++++++++++++++----- 1 file changed, 18 insertions(+), 6 deletions(-) diff --git a/BE/src/main/java/org/team4/airbnb/auth/OAuthService.java b/BE/src/main/java/org/team4/airbnb/auth/OAuthService.java index 88de565f7..7f697fd1f 100644 --- a/BE/src/main/java/org/team4/airbnb/auth/OAuthService.java +++ b/BE/src/main/java/org/team4/airbnb/auth/OAuthService.java @@ -14,18 +14,17 @@ import org.team4.airbnb.auth.config.OauthProvider; import org.team4.airbnb.auth.dto.OauthTokenResponse; import org.team4.airbnb.auth.dto.UserProfile; +import org.team4.airbnb.customer.Customer; +import org.team4.airbnb.customer.CustomerRepository; +import org.team4.airbnb.exception.GithubInfoNotFoundException; @RequiredArgsConstructor @Service public class OAuthService { private final MemoryProviderRepository memoryProviderRepository; + private final CustomerRepository customerRepository; - /*Todo - 1. authcode를 통해 OAuth서버에서 access token 얻어오기 - 2. access token을 통해 유저 정보 얻어오기 - 3. 유저 DB에 저장 - */ public LoginResponse processLogin(String provider, String authCode) { OauthProvider oauthProvider = memoryProviderRepository.findByProviderName(provider); @@ -36,13 +35,26 @@ public LoginResponse processLogin(String provider, String authCode) { UserProfile userProfile = getUserInfoFromOauth(provider, oauthProvider, tokenResponse); + if (userProfile.getUserId() == null) { + new GithubInfoNotFoundException(); + } + //3. DB에 유저 정보 저장 (최초 로그인 시 1번) + Customer customer = Customer.of(userProfile.getUserId()); + //기존 db에 사용자 있는지 확인 후, 없다면 DB에 유저 정보 저장 후 로그인처리 + Customer findCustomer = customerRepository.findByUserId() + .orElse(null); + + if(findCustomer == null) { + customer = customerRepository.save(customer); + } return null; } - private OauthTokenResponse getAccessTokenFromOauth(String authCode, OauthProvider oauthProvider) { + private OauthTokenResponse getAccessTokenFromOauth(String authCode, + OauthProvider oauthProvider) { OauthTokenResponse oauthTokenResponse = WebClient.create() .post() .uri(oauthProvider.getTokenUrl())