-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- 유저 정보가 있는 지 확인 후 없으면 id 토큰을 통해 user 정보를 가져와 user_account table에 추가하는 기능 구현
- Loading branch information
1 parent
c299ed5
commit 81eb404
Showing
1 changed file
with
35 additions
and
3 deletions.
There are no files selected for viewing
38 changes: 35 additions & 3 deletions
38
src/main/java/com/example/pnuunivmiryangcampus/service/LoginService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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> 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()); | ||
} | ||
} |