Skip to content
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

[fix] fcmToken 업데이트 & 카카오 unlink #105

Merged
merged 2 commits into from
Jun 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions src/main/java/com/telepigeon/server/domain/User.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,11 @@ public class User {
private User(
String name,
String email,
String fcmToken,
String serialId,
String provider
) {
this.name = name;
this.email = email;
this.fcmToken = fcmToken;
this.serialId = serialId;
this.provider = provider;
this.createdAt = LocalDateTime.now();
Expand All @@ -49,10 +47,12 @@ private User(
public static User create(
String name,
String email,
String fcmToken,
String serialId,
String provider) {
return new User(name, email, fcmToken, serialId, provider);
return new User(name, email, serialId, provider);
}

public void updateFcmToken(String fcmToken) {
this.fcmToken = fcmToken;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@ public class AuthService {
@Transactional
public JwtTokensDto login(final String token, final String fcmToken){
SocialUserInfoDto socialUserInfo = kakaoService.getUserInfo(token);
User user = loadOrCreateKakaoUser(socialUserInfo, fcmToken);
User user = loadOrCreateKakaoUser(socialUserInfo);
user.updateFcmToken(fcmToken);
JwtTokensDto tokens = jwtUtil.generateTokens(user.getId());
tokenSaver.save(Token.create(user.getId(), tokens.refreshToken()));
return tokens;
Expand Down Expand Up @@ -58,7 +59,7 @@ public JwtTokensDto reissue(final String Authorization) {
return tokens;
}

private User loadOrCreateKakaoUser(final SocialUserInfoDto socialUserInfo, final String fcmToken) {
private User loadOrCreateKakaoUser(final SocialUserInfoDto socialUserInfo) {
boolean isRegistered = userRetreiver.existBySerialIdAndProvider(
socialUserInfo.serialId(),
"kakao"
Expand All @@ -68,7 +69,6 @@ private User loadOrCreateKakaoUser(final SocialUserInfoDto socialUserInfo, final
User newUser = User.create(
socialUserInfo.name(),
socialUserInfo.email(),
fcmToken,
socialUserInfo.serialId(),
"kakao"
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
import org.springframework.http.HttpStatusCode;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Service;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.RestClient;

@Service
Expand Down Expand Up @@ -39,14 +41,15 @@ public SocialUserInfoDto getUserInfo(final String token) {
public void unlink(final User user) {
RestClient restClient = RestClient.create();

MultiValueMap<String, String> unlinkBody = new LinkedMultiValueMap<>();
unlinkBody.add("target_id_type", "user_id");
unlinkBody.add("target_id", user.getSerialId());

restClient.post()
.uri(uriBuilder -> uriBuilder.path(kakaoUnlinkUrl)
.queryParam("target_id_type", "user_id")
.queryParam("target_id", user.getSerialId())
.build()
)
.uri(kakaoUnlinkUrl)
.contentType(MediaType.APPLICATION_FORM_URLENCODED)
.header(AuthConstant.AUTHORIZATION_HEADER, "KakaoAK " + kakaoAdminKey)
.body(unlinkBody)
.retrieve()
.onStatus(HttpStatusCode::is4xxClientError, (request, response) -> {
throw new BusinessException(BusinessErrorCode.INVALID_KAKAO_ADMIN_KEY);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ class KakaoLoginTest {
void setUp() throws Exception {
token = "validAccessToken";
socialUserInfo = SocialUserInfoDto.of("123456", "테스트", "[email protected]");
user = User.create("123456", "테스트", "fcmToken", "kakao", "[email protected]");
user = User.create("123456", "테스트", "kakao", "[email protected]");

idField = User.class.getDeclaredField("id"); // private인 id를 설정하지 못해 리플렉션 사용
idField.setAccessible(true);
Expand Down
Loading