Skip to content

Commit

Permalink
#29 feat: 회원가입 기능 구현
Browse files Browse the repository at this point in the history
- 유저 정보가 있는 지 확인 후 없으면 id 토큰을 통해 user 정보를 가져와 user_account table에 추가하는 기능 구현
  • Loading branch information
ki-met-hoon committed Mar 17, 2024
1 parent c299ed5 commit 81eb404
Showing 1 changed file with 35 additions and 3 deletions.
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());
}
}

0 comments on commit 81eb404

Please sign in to comment.