Skip to content

Commit

Permalink
feat: Kakao Login 시 Profile Image를 받아온다. (#20)
Browse files Browse the repository at this point in the history
  • Loading branch information
rlarltj committed Aug 7, 2024
1 parent 34b22f6 commit 2c44c15
Show file tree
Hide file tree
Showing 8 changed files with 40 additions and 37 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
Expand All @@ -27,9 +27,8 @@ public class AuthController {
private final OAuthService oAuthService;
private final UserService userService;


@Operation(summary = "로그인")
@GetMapping("/sign-in")
@PostMapping("/sign-in")
public ResponseEntity<Tokens> signIn(@RequestBody LoginRequest loginRequest) {
OAuthUserDataResponse oAuthUserData = oAuthService.login(loginRequest);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,21 +16,23 @@ public class KakaoUserData {
@Getter
@NoArgsConstructor
static class KakaoAccount {
private String email;
private KakaoProfile profile;
}

@Getter
@NoArgsConstructor
static class KakaoProfile {
private String nickname;
}

public String getEmail() {
return kakaoAccount.getEmail();
@JsonProperty("thumbnail_image_url")
private String profileImage;
}

public String getNickname() {
return kakaoAccount.getProfile().getNickname();
}

public String getProfileImage() {
return kakaoAccount.getProfile().getProfileImage();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,6 @@
public class OAuthUserDataResponse {
private String provider;
private String oauthId;
private String email;
private String profileImageUrl;
private String nickname;
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,14 @@ public class OAuthUserInfo {
private String provider;
private String oauthId;
private String nickname;
private String email;
private String appleRefreshToken;
private String profileImageUrl;

public static OAuthUserInfo from(OAuthUserDataResponse oAuthUserDataResponse) {
return OAuthUserInfo.builder()
.provider(oAuthUserDataResponse.getProvider())
.oauthId(oAuthUserDataResponse.getOauthId())
.nickname(oAuthUserDataResponse.getNickname())
.email(oAuthUserDataResponse.getEmail())
.profileImageUrl(oAuthUserDataResponse.getProfileImageUrl())
.build();
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,22 +48,23 @@ public OAuthUserDataResponse getOAuthUserData(OAuthUserDataRequest request) {

try {
ResponseEntity<KakaoUserData> response = restTemplate.exchange(
url,
HttpMethod.GET,
httpRequest,
KakaoUserData.class
url,
HttpMethod.GET,
httpRequest,
KakaoUserData.class
);
assert response.getBody() != null;

KakaoUserData userData = response.getBody();

return OAuthUserDataResponse.builder()
.provider(getAuthProvider().toString())
.oauthId(userData.getId().toString())
.email(userData.getEmail())
.nickname(userData.getNickname())
.build();
.provider(getAuthProvider().toString())
.profileImageUrl(userData.getProfileImage())
.oauthId(userData.getId().toString())
.nickname(userData.getNickname())
.build();

} catch (RestClientException e) {
} catch (Exception e) {
log.warn("[KakaoService] failed to get OAuth User Data = {}", request.getAccessToken());

if (e instanceof RestClientResponseException) {
Expand Down
20 changes: 11 additions & 9 deletions src/main/java/com/dnd/accompany/domain/user/entity/User.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,6 @@ public class User extends TimeBaseEntity {
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;

@Column(nullable = false)
private String email;

@Column(nullable = false)
private String nickname;

Expand All @@ -46,12 +43,17 @@ public class User extends TimeBaseEntity {
)
private String oauthId;

public static User of(String email, String nickname, String provider, String oauthId) {
@Column(length = 1000)
private String profileImageUrl;

private boolean deleted = false;

public static User of(String nickname, String provider, String oauthId, String profileImageUrl) {
return User.builder()
.email(email)
.nickname(nickname)
.provider(provider)
.oauthId(oauthId)
.build();
.nickname(nickname)
.provider(provider)
.oauthId(oauthId)
.profileImageUrl(profileImageUrl)
.build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,10 @@ public AuthUserInfo getOrRegister(OAuthUserInfo oauthUserInfo) {
@Transactional
public User registerUser(OAuthUserInfo oauthUserInfo) {
return userRepository.save(User.of(
oauthUserInfo.getEmail(),
oauthUserInfo.getNickname(),
oauthUserInfo.getProvider(),
oauthUserInfo.getOauthId()
oauthUserInfo.getOauthId(),
oauthUserInfo.getProfileImageUrl()
));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ void setup() {
.provider("KAKAO")
.nickname("TESTER1")
.oauthId("KA-123")
.email("[email protected]")
.profileImageUrl("https://")
.build();

oauthUserInfo = OAuthUserInfo.from(oAuthUserDataResponse);
Expand All @@ -50,10 +50,10 @@ void setup() {
void success() {
//given
User newUser = User.of(
oauthUserInfo.getEmail(),
oauthUserInfo.getNickname(),
oauthUserInfo.getProvider(),
oauthUserInfo.getOauthId()
oauthUserInfo.getOauthId(),
oauthUserInfo.getProfileImageUrl()
);

ReflectionTestUtils.setField(newUser, "id", 1L);
Expand All @@ -74,10 +74,10 @@ void success() {
void success2() {
//given
User existingUser = User.of(
oauthUserInfo.getEmail(),
oauthUserInfo.getNickname(),
oauthUserInfo.getProvider(),
oauthUserInfo.getOauthId()
oauthUserInfo.getOauthId(),
oauthUserInfo.getProfileImageUrl()
);

ReflectionTestUtils.setField(existingUser, "id", 1L);
Expand Down

0 comments on commit 2c44c15

Please sign in to comment.