Skip to content

Commit

Permalink
[FEAT] 사용자 정보에 카카오 토큰 저장 (#171)
Browse files Browse the repository at this point in the history
* refactor: JsonNaming으로 SnakeCase 전략 적용

- 각 필드마다 적용했던 `@JsonProperty` 어노테이션을 제거해 코드 간략화

* feat: Member 엔티티에 kakaoToken 필드 추가

* feat: 소셜 로그인 시 카카오 토큰을 회원 정보에 저장
  • Loading branch information
Profile-exe authored Sep 28, 2024
1 parent 495b4c2 commit 37174e8
Show file tree
Hide file tree
Showing 8 changed files with 54 additions and 34 deletions.
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
package econo.buddybridge.auth.dto.kakao;

import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.databind.PropertyNamingStrategies;
import com.fasterxml.jackson.databind.annotation.JsonNaming;
import lombok.Builder;

@Builder
public record KakaoErrorResponse (
@JsonNaming(PropertyNamingStrategies.SnakeCaseStrategy.class)
public record KakaoErrorResponse(
String error,
@JsonProperty("error_description")
String errorDescription,
@JsonProperty("error_code")
String errorCode
) {
}

}
Original file line number Diff line number Diff line change
@@ -1,44 +1,40 @@
package econo.buddybridge.auth.dto.kakao;

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.databind.PropertyNamingStrategies;
import com.fasterxml.jackson.databind.annotation.JsonNaming;
import econo.buddybridge.auth.dto.OAuthInfoResponse;
import java.time.LocalDateTime;
import lombok.Data;

@Data
@JsonIgnoreProperties(ignoreUnknown = true)
@JsonNaming(PropertyNamingStrategies.SnakeCaseStrategy.class)
public class KakaoInfoResponse implements OAuthInfoResponse {

@JsonProperty("kakao_account")
private KakaoAccount kakaoAccount;

@Data
@JsonNaming(PropertyNamingStrategies.SnakeCaseStrategy.class)
public static class KakaoAccount {

@JsonProperty("profile")
private KakaoProfile profile;

@JsonProperty("name")
private String name;

@JsonProperty("email")
private String email;

@JsonProperty("birthyear")
private String birthyear;

@JsonProperty("gender")
private String gender;
}

@Data
@JsonNaming(PropertyNamingStrategies.SnakeCaseStrategy.class)
public static class KakaoProfile {

@JsonProperty("nickname")
private String nickname;

@JsonProperty("profile_image_url")
private String profileImageUrl;
}

Expand Down
10 changes: 3 additions & 7 deletions src/main/java/econo/buddybridge/auth/dto/kakao/KakaoTokens.java
Original file line number Diff line number Diff line change
@@ -1,30 +1,26 @@
package econo.buddybridge.auth.dto.kakao;

import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.databind.PropertyNamingStrategies;
import com.fasterxml.jackson.databind.annotation.JsonNaming;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;

@Getter
@Setter
@NoArgsConstructor
@JsonNaming(PropertyNamingStrategies.SnakeCaseStrategy.class)
public class KakaoTokens {

@JsonProperty("token_type")
private String tokenType;

@JsonProperty("access_token")
private String accessToken;

@JsonProperty("expires_in")
private int expiresIn;

@JsonProperty("refresh_token")
private String refreshToken;

@JsonProperty("refresh_token_expires_in")
private int refreshTokenExpiresIn;

@JsonProperty("scope")
private String scope;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package econo.buddybridge.auth.dto.kakao;

import econo.buddybridge.auth.dto.OAuthInfoResponse;

public record UserInfoWithKakaoToken(
OAuthInfoResponse info,
String accessToken
) {

}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import econo.buddybridge.auth.client.OAuthApiClient;
import econo.buddybridge.auth.dto.OAuthInfoResponse;
import econo.buddybridge.auth.dto.OAuthLoginParams;
import econo.buddybridge.auth.dto.kakao.UserInfoWithKakaoToken;
import java.util.EnumMap;
import java.util.List;
import java.util.Map;
Expand All @@ -23,13 +24,13 @@ public OAuthInfoService(List<OAuthApiClient> clients) {
.collect(Collectors.toMap(OAuthApiClient::getOAuthProvider, Function.identity())));
}

public OAuthInfoResponse getUserInfo(OAuthLoginParams params) {
public UserInfoWithKakaoToken getUserInfo(OAuthLoginParams params) {
OAuthApiClient client = clients.get(params.oAuthProvider());

String accessToken = client.getAccessToken(params);
log.info("accessToken: {}", accessToken);
OAuthInfoResponse userInfo = client.getUserInfo(accessToken);

return client.getUserInfo(accessToken);
return new UserInfoWithKakaoToken(userInfo, accessToken);
}

public void logout(OAuthProvider provider) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import econo.buddybridge.auth.OAuthProvider;
import econo.buddybridge.auth.dto.OAuthInfoResponse;
import econo.buddybridge.auth.dto.OAuthLoginParams;
import econo.buddybridge.auth.dto.kakao.UserInfoWithKakaoToken;
import econo.buddybridge.member.dto.MemberResDto;
import econo.buddybridge.member.service.MemberService;
import lombok.RequiredArgsConstructor;
Expand All @@ -16,8 +17,8 @@ public class OAuthLoginService {
private final MemberService memberService;

public MemberResDto login(OAuthLoginParams params) {
OAuthInfoResponse info = OAuthInfoService.getUserInfo(params);
return memberService.findOrCreateMemberByEmail(info);
UserInfoWithKakaoToken userInfo = OAuthInfoService.getUserInfo(params);
return memberService.findOrCreateMemberByEmail(userInfo);
}

public void logout(OAuthProvider provider) {
Expand Down
18 changes: 16 additions & 2 deletions src/main/java/econo/buddybridge/member/entity/Member.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,14 @@
package econo.buddybridge.member.entity;

import econo.buddybridge.common.persistence.BaseEntity;
import jakarta.persistence.*;
import jakarta.persistence.Column;
import jakarta.persistence.Entity;
import jakarta.persistence.EnumType;
import jakarta.persistence.Enumerated;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;
import jakarta.persistence.Table;
import lombok.AccessLevel;
import lombok.Builder;
import lombok.Getter;
Expand Down Expand Up @@ -36,16 +43,23 @@ public class Member extends BaseEntity {
@Enumerated(EnumType.STRING)
private Gender gender;

private String kakaoToken;

@Builder
public Member(String name, String nickname, String profileImageUrl, String email,
Integer age, DisabilityType disabilityType, Gender gender) {
Integer age, DisabilityType disabilityType, Gender gender, String kakaoToken) {
this.name = name;
this.nickname = nickname;
this.profileImageUrl = profileImageUrl;
this.email = email;
this.age = age;
this.disabilityType = disabilityType;
this.gender = gender;
this.kakaoToken = kakaoToken;
}

public void updateKakaoToken(String kakaoToken) {
this.kakaoToken = kakaoToken;
}

public void updateMemberInfo(String name, String nickname, String profileImageUrl, String email, Integer age,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package econo.buddybridge.member.service;

import econo.buddybridge.auth.dto.OAuthInfoResponse;
import econo.buddybridge.auth.dto.kakao.UserInfoWithKakaoToken;
import econo.buddybridge.member.dto.MemberReqDto;
import econo.buddybridge.member.dto.MemberResDto;
import econo.buddybridge.member.entity.Member;
Expand Down Expand Up @@ -35,14 +35,15 @@ public Member findMemberByIdOrThrow(Long memberId) {
}

@Transactional
public MemberResDto findOrCreateMemberByEmail(OAuthInfoResponse info) {
Member member = memberRepository.findByEmail(info.getEmail())
.orElseGet(() -> newMember(info));
public MemberResDto findOrCreateMemberByEmail(UserInfoWithKakaoToken userInfo) {
Member member = memberRepository.findByEmail(userInfo.info().getEmail())
.orElseGet(() -> newMember(userInfo));
return new MemberResDto(member);
}

private Member newMember(OAuthInfoResponse info) {
Member member = info.toMember();
private Member newMember(UserInfoWithKakaoToken userInfo) {
Member member = userInfo.info().toMember();
member.updateKakaoToken(userInfo.accessToken());
return memberRepository.save(member);
}

Expand Down

0 comments on commit 37174e8

Please sign in to comment.