Skip to content

Commit

Permalink
feat: 카카오 계정 탈퇴 시 서비스를 unlink한다. (#6)
Browse files Browse the repository at this point in the history
* feat: kakao adminKey를 프로퍼티에 추가한다.

* feat: 카카오 계정 탈퇴 시 서비스를 unlink한다.
  • Loading branch information
rlarltj authored Jun 20, 2024
1 parent 2c75ff3 commit a4e9da7
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package com.moneymong.global.security.oauth.handler;

import com.moneymong.domain.user.entity.User;
import com.moneymong.domain.user.repository.UserRepository;
import com.moneymong.global.exception.custom.NotFoundException;
import com.moneymong.global.exception.enums.ErrorCode;
import com.moneymong.global.security.oauth.dto.KakaoUserData;
import com.moneymong.global.security.oauth.dto.OAuthUserDataRequest;
Expand All @@ -11,6 +14,8 @@
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.*;
import org.springframework.stereotype.Component;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.RestClientException;
import org.springframework.web.client.RestClientResponseException;
import org.springframework.web.client.RestTemplate;
Expand All @@ -21,10 +26,14 @@
public class KakaoService implements OAuthAuthenticationHandler {

private final RestTemplate restTemplate;
private final UserRepository userRepository;

@Value("${spring.security.oauth2.kakao.host}")
private String host;

@Value("${spring.security.oauth2.kakao.admin-key}")
private String adminKey;

@Override
public OAuthProvider getAuthProvider() {
return OAuthProvider.KAKAO;
Expand Down Expand Up @@ -70,6 +79,39 @@ public OAuthUserDataResponse getOAuthUserData(OAuthUserDataRequest request) {

@Override
public void unlink(Long userId) {
String oauthId = userRepository.findById(userId)
.orElseThrow(() -> new NotFoundException(ErrorCode.USER_NOT_FOUND))
.getOauthId();

String url = host + "/v1/user/unlink";

HttpHeaders httpHeaders = new HttpHeaders();
httpHeaders.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
httpHeaders.add("Authorization", "KakaoAK " + adminKey);

MultiValueMap<String, String> body = new LinkedMultiValueMap<>();
body.add("target_id_type", "user_id");
body.add("target_id", oauthId);

HttpEntity<?> httpRequest = new HttpEntity<>(body, httpHeaders);

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

} catch (RestClientException e) {
log.warn("[KakaoService] failed to unlink User = {}", oauthId);

if (e instanceof RestClientResponseException) {
throw new HttpClientException(ErrorCode.INVALID_OAUTH_TOKEN);
}

throw new HttpClientException(ErrorCode.HTTP_CLIENT_REQUEST_FAILED);
}
}
}
1 change: 1 addition & 0 deletions src/main/resources/application-dev.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ spring:
oauth2:
kakao:
host: https://kapi.kakao.com
admin-key: ${KAKAO_ADMIN_KEY}
apple:
host: https://appleid.apple.com
grant-type: authorization_code
Expand Down
1 change: 1 addition & 0 deletions src/main/resources/application-prod.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ spring:
oauth2:
kakao:
host: https://kapi.kakao.com
admin-key: ${KAKAO_ADMIN_KEY}
apple:
host: https://appleid.apple.com
grant-type: authorization_code
Expand Down

0 comments on commit a4e9da7

Please sign in to comment.