-
Notifications
You must be signed in to change notification settings - Fork 0
[FEAT] 소셜 로그인 API 구현 #60
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
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
40 changes: 40 additions & 0 deletions
40
src/main/java/com/yeogido/backend/domain/auth/client/KakaoUserInfoClient.java
This file contains hidden or 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 |
|---|---|---|
| @@ -0,0 +1,40 @@ | ||
| package com.yeogido.backend.domain.auth.client; | ||
|
|
||
| import com.yeogido.backend.domain.auth.dto.SocialUserInfo; | ||
| import com.yeogido.backend.domain.auth.exception.AuthErrorCode; | ||
| import com.yeogido.backend.global.exception.GeneralException; | ||
| import lombok.RequiredArgsConstructor; | ||
| import org.springframework.beans.factory.annotation.Value; | ||
| import org.springframework.http.HttpHeaders; | ||
| import org.springframework.stereotype.Component; | ||
| import org.springframework.web.client.RestClient; | ||
| import org.springframework.web.client.RestClientException; | ||
|
|
||
| @Component | ||
| @RequiredArgsConstructor | ||
| public class KakaoUserInfoClient { | ||
|
|
||
| private final RestClient.Builder restClientBuilder; | ||
|
|
||
| @Value("${app.oauth.kakao.user-info-uri}") | ||
| private String userInfoUri; | ||
|
|
||
| public SocialUserInfo getUserInfo(String accessToken) { | ||
| try { | ||
| KakaoUserInfoResponse response = restClientBuilder.build() | ||
| .get() | ||
| .uri(userInfoUri) | ||
| .header(HttpHeaders.AUTHORIZATION, "Bearer " + accessToken) | ||
| .retrieve() | ||
| .body(KakaoUserInfoResponse.class); | ||
|
|
||
| if (response == null || response.id() == null) { | ||
| throw new GeneralException(AuthErrorCode.INVALID_SOCIAL_ACCESS_TOKEN); | ||
| } | ||
|
|
||
| return response.toSocialUserInfo(); | ||
| } catch (RestClientException e) { | ||
| throw new GeneralException(AuthErrorCode.INVALID_SOCIAL_ACCESS_TOKEN); | ||
| } | ||
| } | ||
| } |
43 changes: 43 additions & 0 deletions
43
src/main/java/com/yeogido/backend/domain/auth/client/KakaoUserInfoResponse.java
This file contains hidden or 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 |
|---|---|---|
| @@ -0,0 +1,43 @@ | ||
| package com.yeogido.backend.domain.auth.client; | ||
|
|
||
| import com.fasterxml.jackson.annotation.JsonProperty; | ||
| import com.yeogido.backend.domain.auth.dto.SocialUserInfo; | ||
| import com.yeogido.backend.domain.auth.enums.SocialProvider; | ||
|
|
||
| public record KakaoUserInfoResponse( | ||
| Long id, | ||
| @JsonProperty("kakao_account") | ||
| KakaoAccount kakaoAccount, | ||
| Properties properties | ||
| ) { | ||
|
|
||
| public SocialUserInfo toSocialUserInfo() { | ||
| String email = kakaoAccount == null ? null : kakaoAccount.email(); | ||
| String name = kakaoAccount == null ? null : kakaoAccount.name(); | ||
| if (name == null && properties != null) { | ||
| name = properties.nickname(); | ||
| } | ||
| String profileImageUrl = properties == null ? null : properties.profileImage(); | ||
|
|
||
| return new SocialUserInfo( | ||
| SocialProvider.KAKAO, | ||
| String.valueOf(id), | ||
| email, | ||
| name, | ||
| profileImageUrl | ||
| ); | ||
| } | ||
|
|
||
| public record KakaoAccount( | ||
| String email, | ||
| String name | ||
| ) { | ||
| } | ||
|
|
||
| public record Properties( | ||
| String nickname, | ||
| @JsonProperty("profile_image") | ||
| String profileImage | ||
| ) { | ||
| } | ||
| } |
40 changes: 40 additions & 0 deletions
40
src/main/java/com/yeogido/backend/domain/auth/client/NaverUserInfoClient.java
This file contains hidden or 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 |
|---|---|---|
| @@ -0,0 +1,40 @@ | ||
| package com.yeogido.backend.domain.auth.client; | ||
|
|
||
| import com.yeogido.backend.domain.auth.dto.SocialUserInfo; | ||
| import com.yeogido.backend.domain.auth.exception.AuthErrorCode; | ||
| import com.yeogido.backend.global.exception.GeneralException; | ||
| import lombok.RequiredArgsConstructor; | ||
| import org.springframework.beans.factory.annotation.Value; | ||
| import org.springframework.http.HttpHeaders; | ||
| import org.springframework.stereotype.Component; | ||
| import org.springframework.web.client.RestClient; | ||
| import org.springframework.web.client.RestClientException; | ||
|
|
||
| @Component | ||
| @RequiredArgsConstructor | ||
| public class NaverUserInfoClient { | ||
|
|
||
| private final RestClient.Builder restClientBuilder; | ||
|
|
||
| @Value("${app.oauth.naver.user-info-uri}") | ||
| private String userInfoUri; | ||
|
|
||
| public SocialUserInfo getUserInfo(String accessToken) { | ||
| try { | ||
| NaverUserInfoResponse response = restClientBuilder.build() | ||
| .get() | ||
| .uri(userInfoUri) | ||
| .header(HttpHeaders.AUTHORIZATION, "Bearer " + accessToken) | ||
| .retrieve() | ||
| .body(NaverUserInfoResponse.class); | ||
|
|
||
| if (response == null || response.response() == null || response.response().id() == null) { | ||
| throw new GeneralException(AuthErrorCode.INVALID_SOCIAL_ACCESS_TOKEN); | ||
| } | ||
|
|
||
| return response.toSocialUserInfo(); | ||
| } catch (RestClientException e) { | ||
| throw new GeneralException(AuthErrorCode.INVALID_SOCIAL_ACCESS_TOKEN); | ||
| } | ||
| } | ||
| } |
31 changes: 31 additions & 0 deletions
31
src/main/java/com/yeogido/backend/domain/auth/client/NaverUserInfoResponse.java
This file contains hidden or 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 |
|---|---|---|
| @@ -0,0 +1,31 @@ | ||
| package com.yeogido.backend.domain.auth.client; | ||
|
|
||
| import com.fasterxml.jackson.annotation.JsonProperty; | ||
| import com.yeogido.backend.domain.auth.dto.SocialUserInfo; | ||
| import com.yeogido.backend.domain.auth.enums.SocialProvider; | ||
|
|
||
| public record NaverUserInfoResponse( | ||
| String resultcode, | ||
| String message, | ||
| Response response | ||
| ) { | ||
|
|
||
| public SocialUserInfo toSocialUserInfo() { | ||
| return new SocialUserInfo( | ||
| SocialProvider.NAVER, | ||
| response.id(), | ||
| response.email(), | ||
| response.name(), | ||
| response.profileImageUrl() | ||
| ); | ||
| } | ||
|
|
||
| public record Response( | ||
| String id, | ||
| String email, | ||
| String name, | ||
| @JsonProperty("profile_image") | ||
| String profileImageUrl | ||
| ) { | ||
| } | ||
| } |
This file contains hidden or 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
37 changes: 37 additions & 0 deletions
37
src/main/java/com/yeogido/backend/domain/auth/converter/AuthConverter.java
This file contains hidden or 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 |
|---|---|---|
| @@ -0,0 +1,37 @@ | ||
| package com.yeogido.backend.domain.auth.converter; | ||
|
|
||
| import com.yeogido.backend.domain.auth.dto.AuthResDTO; | ||
| import com.yeogido.backend.domain.auth.dto.SocialUserInfo; | ||
|
|
||
| public class AuthConverter { | ||
|
|
||
| private AuthConverter() { | ||
| } | ||
|
|
||
| public static AuthResDTO.SocialLogin toExistingSocialLoginResponse(AuthResDTO.Token token) { | ||
| return new AuthResDTO.SocialLogin( | ||
| false, | ||
| token.userId(), | ||
| token.accessToken(), | ||
| token.refreshToken(), | ||
| null, | ||
| null, | ||
| null | ||
| ); | ||
| } | ||
|
|
||
| public static AuthResDTO.SocialLogin toNewSocialLoginResponse( | ||
| String temporaryToken, | ||
| SocialUserInfo socialUserInfo | ||
| ) { | ||
| return new AuthResDTO.SocialLogin( | ||
| true, | ||
| null, | ||
| null, | ||
| null, | ||
| temporaryToken, | ||
| socialUserInfo.email(), | ||
| socialUserInfo.name() | ||
| ); | ||
| } | ||
| } |
This file contains hidden or 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
This file contains hidden or 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
22 changes: 22 additions & 0 deletions
22
src/main/java/com/yeogido/backend/domain/auth/dto/SocialSignupTokenPayload.java
This file contains hidden or 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 |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| package com.yeogido.backend.domain.auth.dto; | ||
|
|
||
| import com.yeogido.backend.domain.auth.enums.SocialProvider; | ||
|
|
||
| public record SocialSignupTokenPayload( | ||
| SocialProvider provider, | ||
| String providerId, | ||
| String email, | ||
| String name, | ||
| String profileImageUrl | ||
| ) { | ||
|
|
||
| public static SocialSignupTokenPayload from(SocialUserInfo socialUserInfo) { | ||
| return new SocialSignupTokenPayload( | ||
| socialUserInfo.provider(), | ||
| socialUserInfo.providerId(), | ||
| socialUserInfo.email(), | ||
| socialUserInfo.name(), | ||
| socialUserInfo.profileImageUrl() | ||
| ); | ||
| } | ||
| } |
12 changes: 12 additions & 0 deletions
12
src/main/java/com/yeogido/backend/domain/auth/dto/SocialUserInfo.java
This file contains hidden or 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 |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| package com.yeogido.backend.domain.auth.dto; | ||
|
|
||
| import com.yeogido.backend.domain.auth.enums.SocialProvider; | ||
|
|
||
| public record SocialUserInfo( | ||
| SocialProvider provider, | ||
| String providerId, | ||
| String email, | ||
| String name, | ||
| String profileImageUrl | ||
| ) { | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.