-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weโll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Feat/#14 social login kakao #15
base: develop
Are you sure you want to change the base?
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
์๊ณ ํ์ จ์ต๋๋ค! ! ๐๐๐
import org.springframework.web.bind.annotation.GetMapping; | ||
import org.springframework.web.bind.annotation.RequestHeader; | ||
|
||
@FeignClient(name = "kakaoAuth", url = "https://kapi.kakao.com") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
์ฌ๊ธฐ url
์ yml์ ๋ฐ๋ก ๋นผ์ฃผ๋ฉด ์ข์ ๊ฑฐ ๊ฐ์์!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
๋ต!
return kakaoUser; | ||
} | ||
|
||
private boolean checkSocialUser(KakaoUserResponseDto kakaoUser){ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
๋ฆฌํด๊ฐ์ ์ฌ์ฉํ์ง ์์ผ๋๊น void๋ ์ด๋ค๊ฐ์?!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
์์ฃผ ์ข์ต๋๋ค.
private KakaoUser checkSocialUserHaveEmail(KakaoUserResponseDto kakaoUserResponseDto){ | ||
String email = ""; | ||
if (kakaoUserResponseDto.kakao_account().email() != null) { | ||
email = kakaoUserResponseDto.kakao_account().email(); | ||
} | ||
return KakaoUser.of(kakaoUserResponseDto.id(), email); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
๋์๊ผผ๊ผผ!!! ๐๐๐๐
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
์จ๋๊ฐ ๋
ธ๋๋ก ๊ตฌํํ๊ฑฐ ๊ทธ๋๋ก ๋ก์ง ์์ฑํ๊ฑฐ์~
์ฒด๊ณ ์ง์
AuthSocialCheckResponseDto response ; | ||
if (existUser==null) | ||
response = AuthSocialCheckResponseDto.of(user.userId(),user.email(),false); | ||
else{ | ||
response = AuthSocialCheckResponseDto.of(user.userId(),user.email(),true); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
๐๐
import java.util.Optional; | ||
|
||
public interface AuthenticationProviderRepository extends JpaRepository<AuthenticationProvider, Long> { | ||
@EntityGraph(attributePaths = "user") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
์์๊ฐ์! ๐
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
๊ฐ์ฌํด์!!
Optional<AuthenticationProvider> authenticationProvider = authenticationProviderRepository | ||
.findByIdAndProvider(kakaoUser.userId(), | ||
kakaoUser.providerType()); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
์ด ๋ถ๋ถ ํ ์ค์ด์ด๋ ๋ ๊ฑฐ ๊ฐ์์!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
๋ต ๊ฐ๋ ์ฑ๋๋ฌธ์ ์ฌ๋ฌ์ค๋ก ์ป๋๋ฐ, ํ์ค๋ก ๊ณ ์น๊ฒ ์ด๋น~
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
์ฝ๋ ์ ๋ง ์๋ดค์ต๋๋ค~!! FeignClient ํฅ๋ฏธ๋กญ๋ค์ฉ!!
์ ๊ฐ ๋ฉ์
๋จ๊ฒจ๋์ ๋ถ๋ถ ์๊ฒฌ ์ฃผ์ธ์~! ๋ก์ง์ ๋ค ์ ๋ง ์ข์๋ฐ, ์กฐ๊ธ ์ง์ ๋ถํด๋ณด์ผ ์ ์๋ ๋ถ๋ถ๋ค ์์ ํ๋ฉด ์ข์๊ฑฐ ๊ฐ์์
return kakaoUser; | ||
} | ||
|
||
private boolean checkSocialUser(KakaoUserResponseDto kakaoUser){ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
chckSocialUser๋ Checkํ๋ค๋ ์ญํ ๋ง ๋ถ์ฌํ๊ณ (true / false ๋ฐํ) ์์ getKakaoUser์์ throwํ๋๊ฒ ๋ ๋ฐ๋์งํ ๊ฑฐ ๊ฐ์์! ๋ฉ์๋์ ์ด๋ฆ๊ณผ ์ญํ ์ด ๋ช ํํ๊ฒ ์ค์ํ๋ค ์๊ฐํด์ ๋จ๊น๋๋ค!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
๋ต! ๊ฐ์ฌํฉ๋๋ค!
return true; | ||
} | ||
|
||
private KakaoUser checkSocialUserHaveEmail(KakaoUserResponseDto kakaoUserResponseDto){ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
email ๋ฑ๋ก๋์ด์์ ๊ฐ๋ค ์ฐ๊ณ , ์์ผ๋ฉด ์์ฐ๋ ํ์์ผ๊น์?!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
๋ต๋ง์ต๋๋น!
} | ||
} | ||
|
||
public KakaoUser getKakaoUser(String accessToken){ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
์ด๊ฑฐ๋ ์ด๋์ ์ฐ๋๊ฑธ๊น์? interface๋ฅผ ๋ง๋ค์์ผ๋ฉด Overrideํด์ผ ์๋ฏธ๊ฐ ์๋ค๊ณ ์๊ฐํฉ๋๋ค!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
authService -> kakao->check์ ์ฐ์
๋๋ค!
kakaoAuth๋ฅผ interface๋ก ๊ตฌํํ ์ด์ ๋ ,@FeignClient์ ๋ฌธ๋ฒ๋๋ฌธ์ด๊ณ ,
kakaoUser์ ๊ฐ๊ณ ์ค๋ ๋ก์ง์ ํ ํ์ผ๋ก ์ฎ๊ธฐ์ํด KakaoAuthImpl์ ๋ง๋ค์์ต๋๋น,
์ธํฐํ์ด์ค๋ฅผ implementํ ์ด์ ๋ @FeignClient๋ฅผ ์ฌ์ฉํ๊ธฐ ์ํด์์
๋๋ค!
@@ -33,4 +35,17 @@ public ResponseEntity<ApiResponse> signInUser(@RequestBody AuthSignInRequestDto | |||
AuthSignInResponseDto response = authService.signInUser(request); | |||
return ResponseEntity.ok(ApiResponse.success(SUCCESS_SIGN_IN.getMessage(), response)); | |||
} | |||
|
|||
@PostMapping("/kakao/check") | |||
public ResponseEntity<ApiResponse> findSocialUser(@RequestBody AuthSocialCheckRequestDto request) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
๋ง์ํ์ ๋๋ก ํด๋น ๋ก์ง ๋ค service๋ก ๋๊ธฐ๋๊ฒ ๋ง๋ค๊ณ ์๊ฐํด์.
getUser + findByKey ๋ก์ง๋ ํฉ์ณ์ ๋ฐํํ๋๋ก ํฉ์๋ค!!! ์์ธ์ฒ๋ฆฌ๋ Service์์ ํ๊ณ ์ฉ~! ์ปจํธ๋กค๋ฌ๋ ์ ๋ฌ์๋ง ์ ๊ฒฝ์ฐ๋๋ก ํฉ์๋น
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
๋ต์๊ฒ ์ต๋๋น!
import java.util.Optional; | ||
|
||
public interface AuthenticationProviderRepository extends JpaRepository<AuthenticationProvider, Long> { | ||
@EntityGraph(attributePaths = "user") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
์ด๊ฑฐ ์ด๋ค ์ด๋ ธํ ์ด์ ์ธ์ง ์ ๋ชฐ๋ผ์ ๊ฐ๋ตํ๊ฒ ์ค๋ช ๋ถํ๋๋ ค๋ ๋ ๊น์ฉ?!~์๋ก ๋ฐฐ์ด๋ค ์ผํธ!
|
||
public User findByKey(KakaoUser kakaoUser){ | ||
Optional<AuthenticationProvider> authenticationProvider = authenticationProviderRepository | ||
.findByIdAndProvider(kakaoUser.userId(), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
์ด๊ฑฐ ๊ฐํ์ด ์์ด๋ฌ์ฃ ใ ใ ใ ํใ ใ ใ ํํซ..
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
์ ์ผ๋ถ๋ก ์๋ณด์ผ๊ฒ๊ฐ์์ ์ด๋ ๊ฒ ํ๋๋ฐ ํ์ค๋ก ํฉ์น๊ฒ ์ด๋น~
.findByIdAndProvider(kakaoUser.userId(), | ||
kakaoUser.providerType()); | ||
// ์นด์นด์ค ๊ณ์ ์ ํ์ธ, ์ฐ๋ฆฌ ์๋น์ค์ ์ด๋ฏธ ๋ก๊ทธ์ธํจ | ||
if(authenticationProvider.isPresent()) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
๋ง์๋๋ ธ๋ฏ์ด ์์ getUser์ findByKey ํฉ์น๋๊ฒ ์ข์๊ฑฐ ๊ฐ์์!
response = AuthSocialCheckResponseDto.of(user.userId(),user.email(),false);
response = AuthSocialCheckResponseDto.of(user.userId(),user.email(),true);
์ด๊ฑฐ ์ฐจ์ด๊ฐ true, false๋ฐ์ ์๋๋ฐ, isPresent์ผ๋ true DTO ๋ฐํํ๊ณ ์๋๋๋ false DTO ๋ฐํํ๋ฉด ๋ ๊ฑฐ ๊ฐ์์!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
๋ต!!! ๊ฐ์ฌํฉ๋๋ค!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
์๊ณ ํ์ จ์ต๋๋ค ! ! ! ๐๐
๐ผ๏ธ ์์ ํ ๋ด์ฉ
[์นด์นด์ค ์ค๋ณต ์ฒดํฌ]๋ฅผ ๊ตฌํํ์ต๋๋ค.
๐ธ PR Point
๋ก์ง flow๋ฅผ ๋ ธ๋๋ ๋๊ฐ์ด ํด๋ดค์ต๋๋ค. ๊ทธ๋ฌ๋ค๋ณด๋ ์ปจํธ๋กค๋ฌ์์ data๋ฅผ ๋ฆฌํดํ๋๋ถ๋ถ์ด, if-else๋ก์ง์ด ๋ค์ด๊ฐ์ ์ข ์ง์ ๋ถ ํด์ง๋ ๊ฒ ๊ฐ์ต๋๋ค.
-> ์ด ๋ถ๋ถ์ ์๋น์ค ๋ก์ง์ผ๋ก ์ฎ๊ธฐ๋ ๊ฒ์ ์ด๋ ์ค๋ จ์ง์ ?
@FeignClient ๋ฅผ ์ฌ์ฉํ์ฌ kakaoApi ์๋ฒ์ ํต์ ํด๋ดค์ต๋๋ค.
kakaoAuth๊ด๋ จํ ๋ก์ง์ AuthService์ ๋ฃ์ผ๋ฉด ์ฝ๋๊ฐ ์ง์ ๋ถํด์ ธ์, kakaoAuth๋ฅผ implementationํ KakaoAuthImplํด๋์ค๋ฅผ ๊ตฌํํ์ฌ kakaoAuth์ ํ์ํ ๋ฉ์๋๋ฅผ ๋ฌถ์ด๋จ์ต๋๋ค.
๋ํ ์ด์๊ฐ ์์๋๋ฐ, Authentication์์ ProviderType์ด spring์ enumํ์ ์ผ๋ก ๋์ด, DB ์ซ์๋ก ์ธ์๋๊ธฐ ๋๋ฌธ์, String์ผ๋ก ๋ณํํด์ฃผ๋ ์ด๋ ธํ ์ด์ ์ AuthenticationProvider, ProviderType์ ์ถ๊ฐํ์ฌ ์ด์๋ฅผ ํด๊ฒฐํ์ต๋๋ค.
๋ํ AuthenticationProvider๋ฅผ Spring jpa ๋ฉ์๋๋ฅผ ํตํด ๊ฐ์ ธ์ฌ ๋ userํ๋กํผํฐ์ ์ง์ฐ๋ก๋ฉ์ผ๋ก ์ธํด, ํ๋ก์ ๊ฐ์ฒด๊ฐ ๊ฐ์ ธ์์ ธ, AuthenticationProviderRepository์์ fetchjoin์ ํตํด ๊ฐ๊ณ ์ฌ ์ ์๊ฒ ๊ตฌํํ์ต๋๋ค.
โ๏ธ ๊ด๋ จ ์ด์