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

#32 회원 탈퇴 api #37

Merged
merged 6 commits into from
Sep 5, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,15 @@
import com.dnd.dndtravel.auth.controller.request.AppleLoginRequest;
import com.dnd.dndtravel.auth.service.dto.response.TokenResponse;
import com.dnd.dndtravel.auth.service.dto.response.ReissueTokenResponse;
import com.dnd.dndtravel.config.AuthenticationMember;
import com.dnd.dndtravel.member.domain.Member;
import com.dnd.dndtravel.member.service.MemberService;

import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;

import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.*;

@RequiredArgsConstructor
@RestController
Expand Down Expand Up @@ -50,20 +50,15 @@ public ReissueTokenResponse reissueToken(@RequestBody ReIssueTokenRequest reissu
return jwtTokenService.reIssue(reissueTokenRequest.refreshToken());
}

@PostMapping("/withdraw")
public ResponseEntity<Void> withdraw(@RequestBody AppleWithdrawRequest withdrawRequest) {
@DeleteMapping("/withdraw")
public void withdraw(@Valid @RequestBody AppleWithdrawRequest withdrawRequest, AuthenticationMember authenticationMember) {
// 1. Apple 서버에서 Access Token 받아오기
String accessToken = appleOAuthService.getAccessToken(withdrawRequest.authorizationCode());

// 2. Apple 서버에 탈퇴 요청
boolean revokeSuccessful = appleOAuthService.revoke(accessToken);
appleOAuthService.revoke(accessToken);

if (revokeSuccessful) {
// 3. 자체 회원 탈퇴 진행
memberService.withdrawMember(withdrawRequest.memberId());
return ResponseEntity.ok().build();
} else {
return ResponseEntity.badRequest().build();
}
// 3. 자체 회원 탈퇴 진행
memberService.withdrawMember(authenticationMember.id());
}
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
package com.dnd.dndtravel.auth.controller.request;

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

/*
클라이언트에서 서버로 보내는 요청
*/
public record AppleWithdrawRequest(
String authorizationCode,
long memberId
@NotBlank(message = "인증 코드가 존재하지 않습니다.")
@Size(max = 12, message = "인증 코드가 12자를 초과하였습니다.")
min-0 marked this conversation as resolved.
Show resolved Hide resolved
String authorizationCode
) {
}
4 changes: 2 additions & 2 deletions src/main/java/com/dnd/dndtravel/auth/service/AppleClient.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.dnd.dndtravel.auth.service;

import com.dnd.dndtravel.auth.controller.request.AppleRevokeRequest;
import com.dnd.dndtravel.auth.service.dto.response.AppleRevokeResponse;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
Expand Down Expand Up @@ -30,7 +30,7 @@ AppleSocialTokenInfoResponse getIdToken(
);

@PostMapping("/auth/revoke")
AppleRevokeRequest revoke(
AppleRevokeResponse revoke(
@RequestParam("client_id") String clientId,
@RequestParam("client_secret") String clientSecret,
@RequestParam("token") String refreshToken,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.dnd.dndtravel.auth.service;

import com.dnd.dndtravel.auth.controller.request.AppleRevokeRequest;
import com.dnd.dndtravel.auth.service.dto.response.AppleSocialTokenInfoResponse;
import io.jsonwebtoken.JwsHeader;
import io.jsonwebtoken.Jwts;
Expand Down Expand Up @@ -98,24 +97,16 @@ public String getAccessToken(String authorizationCode) {
return tokenInfo.accessToken();
}

public boolean revoke(String accessToken) {
public void revoke(String accessToken) {
try {
AppleRevokeRequest revokeRequest = new AppleRevokeRequest(
appleClient.revoke(
appleProperties.getClientId(),
generateClientSecret(),
accessToken,
"access_token"
);
appleClient.revoke(
revokeRequest.clientId(),
revokeRequest.clientSecret(),
revokeRequest.Token(),
revokeRequest.tokenType()
);
return true;
} catch (Exception e) {
log.error("Error revoking Apple token", e);
return false;
throw new RuntimeException("Error revoking Apple token", e);
min-0 marked this conversation as resolved.
Show resolved Hide resolved
}
}
}
Original file line number Diff line number Diff line change
@@ -1,17 +1,14 @@
package com.dnd.dndtravel.auth.controller.request;
package com.dnd.dndtravel.auth.service.dto.response;

import com.fasterxml.jackson.annotation.JsonProperty;

/*
서버에서 Apple 서버로 보내는 요청
*/
public record AppleRevokeRequest(
public record AppleRevokeResponse(
@JsonProperty("client_id")
String clientId,
@JsonProperty("client_secret")
String clientSecret,
@JsonProperty("token")
String Token,
String token,
@JsonProperty("token_type_hint")
String tokenType
) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.dnd.dndtravel.member.service;

import com.dnd.dndtravel.auth.repository.RefreshTokenRepository;
import com.dnd.dndtravel.map.repository.MemberAttractionRepository;
import com.dnd.dndtravel.map.repository.MemberRegionRepository;
import com.dnd.dndtravel.member.domain.Member;
Expand All @@ -15,6 +16,7 @@ public class MemberService {
private final MemberRepository memberRepository;
private final MemberAttractionRepository memberAttractionRepository;
private final MemberRegionRepository memberRegionRepository;
private final RefreshTokenRepository refreshTokenRepository;

@Transactional
public Member saveMember(String name, String email, String selectedColor) {
Expand All @@ -30,5 +32,6 @@ public void withdrawMember(long memberId) {
memberRepository.delete(member);
memberAttractionRepository.deleteById(memberId);
memberRegionRepository.deleteById(memberId);
refreshTokenRepository.deleteById(memberId);
}
}
Loading