From 81eb4047267fead28e7cbf0017199f714e51dc5c Mon Sep 17 00:00:00 2001 From: KimSehoon Date: Sun, 17 Mar 2024 22:54:30 +0900 Subject: [PATCH] =?UTF-8?q?#29=20feat:=20=ED=9A=8C=EC=9B=90=EA=B0=80?= =?UTF-8?q?=EC=9E=85=20=EA=B8=B0=EB=8A=A5=20=EA=B5=AC=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 유저 정보가 있는 지 확인 후 없으면 id 토큰을 통해 user 정보를 가져와 user_account table에 추가하는 기능 구현 --- .../service/LoginService.java | 38 +++++++++++++++++-- 1 file changed, 35 insertions(+), 3 deletions(-) diff --git a/src/main/java/com/example/pnuunivmiryangcampus/service/LoginService.java b/src/main/java/com/example/pnuunivmiryangcampus/service/LoginService.java index e88b6e9..b33edd8 100644 --- a/src/main/java/com/example/pnuunivmiryangcampus/service/LoginService.java +++ b/src/main/java/com/example/pnuunivmiryangcampus/service/LoginService.java @@ -1,26 +1,58 @@ package com.example.pnuunivmiryangcampus.service; -import com.example.pnuunivmiryangcampus.auth.KakaoApiCaller; +import com.example.pnuunivmiryangcampus.auth.KakaoInfoClient; +import com.example.pnuunivmiryangcampus.auth.KakaoInformationResponse; +import com.example.pnuunivmiryangcampus.auth.KakaoOauthClient; import com.example.pnuunivmiryangcampus.auth.KakaoProperties; +import com.example.pnuunivmiryangcampus.auth.OIDCDecodePayload; +import com.example.pnuunivmiryangcampus.auth.OauthOIDCHelper; +import com.example.pnuunivmiryangcampus.domain.UserAccount; import com.example.pnuunivmiryangcampus.dto.KakaoTokenDto; +import com.example.pnuunivmiryangcampus.dto.UserAccountDto; +import com.example.pnuunivmiryangcampus.repository.UserAccountRepository; +import java.util.Optional; import lombok.RequiredArgsConstructor; +import lombok.extern.slf4j.Slf4j; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; +@Slf4j @RequiredArgsConstructor @Transactional @Service public class LoginService { - private final KakaoApiCaller kakaoApiCaller; + private final KakaoOauthClient kakaoOauthClient; + private final KakaoInfoClient kakaoInfoClient; private final KakaoProperties kakaoProperties; + private final OauthOIDCHelper oauthOIDCHelper; + private final UserAccountRepository userAccountRepository; public KakaoTokenDto getKakaoToken(String code) { - return kakaoApiCaller.getKakaoToken( + + return kakaoOauthClient.getKakaoToken( kakaoProperties.getRestApiKey(), kakaoProperties.getRedirectUri(), code, kakaoProperties.getGrantType() ); } + + public void isUserRegistered(KakaoTokenDto kakaoTokenDto) { + + OIDCDecodePayload oidcDecodePayload = oauthOIDCHelper.getKakaoOIDCDecodePayload(kakaoTokenDto.idToken()); + Optional userAccount = userAccountRepository.findBySub(oidcDecodePayload.sub()); + + if (userAccount.isEmpty()) { + saveUserAccount(kakaoTokenDto); + } + } + + private void saveUserAccount(KakaoTokenDto kakaoTokenDto) { + + KakaoInformationResponse kakaoInformationResponse = kakaoInfoClient.kakaoUserInfo(kakaoTokenDto.tokenType() + " " + kakaoTokenDto.accessToken()); + UserAccountDto userAccountDto = UserAccountDto.of(kakaoInformationResponse.nickname(), kakaoInformationResponse.email(), kakaoInformationResponse.sub()); + + userAccountRepository.save(userAccountDto.toEntity()); + } }