Skip to content

Commit

Permalink
[MERGE] Merge pull request #222 from Team-WSS/feat/#58
Browse files Browse the repository at this point in the history
[FEAT] 카카오 회원 탈퇴 기능 구현
  • Loading branch information
Kim-TaeUk authored Nov 11, 2024
2 parents 48b9d1f + 35f29c1 commit 1a5bd3e
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 3 deletions.
12 changes: 12 additions & 0 deletions src/main/java/org/websoso/WSSServer/controller/AuthController.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import org.websoso.WSSServer.dto.auth.LogoutRequest;
import org.websoso.WSSServer.dto.auth.ReissueRequest;
import org.websoso.WSSServer.dto.auth.ReissueResponse;
import org.websoso.WSSServer.dto.user.WithdrawalRequest;
import org.websoso.WSSServer.oauth2.service.AppleService;
import org.websoso.WSSServer.oauth2.service.KakaoService;
import org.websoso.WSSServer.service.AuthService;
Expand Down Expand Up @@ -63,4 +64,15 @@ public ResponseEntity<Void> logout(Principal principal,
.status(NO_CONTENT)
.build();
}

@PostMapping("/auth/withdraw")
public ResponseEntity<Void> withdrawUser(Principal principal,
@Valid @RequestBody WithdrawalRequest withdrawalRequest) {
User user = userService.getUserOrException(Long.valueOf(principal.getName()));
String refreshToken = withdrawalRequest.refreshToken();
kakaoService.unlinkFromKakao(user, refreshToken);
return ResponseEntity
.status(NO_CONTENT)
.build();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package org.websoso.WSSServer.dto.user;

import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.Size;

public record WithdrawalRequest(
@Size(max = 80, message = "탈퇴 사유는 80자를 초과할 수 없습니다.")
String reason,

@NotBlank
String refreshToken
) {
}
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,9 @@ public class KakaoService {
@Value("${kakao.admin-key}")
private String kakaoAdminKey;

@Value("${kakao.unlink-url}")
private String kakaoUnlinkUrl;

public AuthResponse getUserInfoFromKakao(String kakaoAccessToken) {
RestClient restClient = RestClient.create();

Expand Down Expand Up @@ -92,10 +95,32 @@ public void kakaoLogout(User user) {
.header(HttpHeaders.AUTHORIZATION, "KakaoAK " + kakaoAdminKey)
.body(logoutInfoBodies)
.retrieve()
.onStatus(HttpStatusCode::is4xxClientError, (request, response) -> {
throw new CustomKakaoException(INVALID_KAKAO_ACCESS_TOKEN,
"Invalid access token for Kakao logout");
.onStatus(HttpStatusCode::is5xxServerError, (request, response) -> {
throw new CustomKakaoException(KAKAO_SERVER_ERROR,
"Kakao server error during logout");
})
.toBodilessEntity();
}

public void unlinkFromKakao(User user, String refreshToken) {
refreshTokenRepository.findByRefreshToken(refreshToken).ifPresent(refreshTokenRepository::delete);

String socialId = user.getSocialId();
String kakaoUserInfoId = socialId.replaceFirst("kakao_", "");

userRepository.delete(user);

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

RestClient.create()
.post()
.uri(kakaoUnlinkUrl)
.header(HttpHeaders.CONTENT_TYPE, "application/x-www-form-urlencoded")
.header(HttpHeaders.AUTHORIZATION, "KakaoAK " + kakaoAdminKey)
.body(withdrawInfoBodies)
.retrieve()
.onStatus(HttpStatusCode::is5xxServerError, (request, response) -> {
throw new CustomKakaoException(KAKAO_SERVER_ERROR,
"Kakao server error during logout");
Expand Down

0 comments on commit 1a5bd3e

Please sign in to comment.