-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
Feat/#34 카카오 로그아웃과 탈퇴 기능 구현
- Loading branch information
Showing
14 changed files
with
145 additions
and
73 deletions.
There are no files selected for viewing
28 changes: 28 additions & 0 deletions
28
src/main/java/com/pyro/yolog/domain/auth/controller/AuthApi.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package com.pyro.yolog.domain.auth.controller; | ||
|
||
import io.swagger.v3.oas.annotations.Operation; | ||
import io.swagger.v3.oas.annotations.Parameter; | ||
import io.swagger.v3.oas.annotations.enums.ParameterIn; | ||
import io.swagger.v3.oas.annotations.responses.ApiResponse; | ||
import io.swagger.v3.oas.annotations.responses.ApiResponses; | ||
import io.swagger.v3.oas.annotations.security.SecurityRequirement; | ||
import io.swagger.v3.oas.annotations.tags.Tag; | ||
|
||
@Tag(name = "Auth") | ||
public interface AuthApi { | ||
|
||
@Operation( | ||
summary = "회원 탈퇴", | ||
description = "회원을 탈퇴합니다. 연결된 소셜과의 연결이 끊깁니다.", | ||
security = {@SecurityRequirement(name = "access_token")} | ||
) | ||
@ApiResponses( | ||
value = { | ||
@ApiResponse( | ||
responseCode = "204", | ||
description = "No Content" | ||
) | ||
} | ||
) | ||
void withdrawMember(); | ||
} |
21 changes: 21 additions & 0 deletions
21
src/main/java/com/pyro/yolog/domain/auth/controller/AuthController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package com.pyro.yolog.domain.auth.controller; | ||
|
||
import com.pyro.yolog.domain.auth.service.AuthService; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.http.HttpStatus; | ||
import org.springframework.web.bind.annotation.DeleteMapping; | ||
import org.springframework.web.bind.annotation.ResponseStatus; | ||
import org.springframework.web.bind.annotation.RestController; | ||
|
||
@RestController | ||
@RequiredArgsConstructor | ||
public class AuthController implements AuthApi{ | ||
private final AuthService authService; | ||
|
||
@ResponseStatus(HttpStatus.NO_CONTENT) | ||
@DeleteMapping("/withdrawal") | ||
@Override | ||
public void withdrawMember() { | ||
authService.withdrawMember(); | ||
} | ||
} |
67 changes: 67 additions & 0 deletions
67
src/main/java/com/pyro/yolog/domain/auth/service/AuthService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
package com.pyro.yolog.domain.auth.service; | ||
|
||
import com.pyro.yolog.domain.member.entity.Member; | ||
import com.pyro.yolog.domain.member.entity.SocialType; | ||
import com.pyro.yolog.domain.member.repository.MemberRepository; | ||
import com.pyro.yolog.global.jwt.service.JwtService; | ||
import com.pyro.yolog.global.query.QueryService; | ||
import jakarta.persistence.EntityNotFoundException; | ||
import lombok.RequiredArgsConstructor; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.springframework.beans.factory.annotation.Value; | ||
import org.springframework.http.*; | ||
import org.springframework.security.core.Authentication; | ||
import org.springframework.security.core.context.SecurityContextHolder; | ||
import org.springframework.security.core.userdetails.UserDetails; | ||
import org.springframework.transaction.annotation.Transactional; | ||
import org.springframework.web.client.RestTemplate; | ||
|
||
@Slf4j | ||
@QueryService | ||
@RequiredArgsConstructor | ||
public class AuthService { | ||
private final MemberRepository memberRepository; | ||
private final JwtService jwtService; | ||
|
||
|
||
@Value("${oauth.kakao.admin-key}") | ||
private String kakaoAdminKey; | ||
|
||
@Value("${oauth.kakao.withdraw-uri}") | ||
private String kakaoWithdrawUri; | ||
|
||
public Long getLoginUserId() { | ||
return getLoginUser().getId(); | ||
} | ||
|
||
public Member getLoginUser() { | ||
Authentication authentication = SecurityContextHolder.getContext().getAuthentication(); | ||
UserDetails userDetails = (UserDetails) authentication.getPrincipal(); | ||
return memberRepository.findByEmail(userDetails.getUsername()).orElseThrow(EntityNotFoundException::new); | ||
} | ||
|
||
|
||
@Transactional | ||
public void withdrawMember() { | ||
Member member = getLoginUser(); | ||
if (member.getSocialType().equals(SocialType.KAKAO)) { | ||
sendWithdrawRequestToKakao(SocialType.KAKAO, member); | ||
} | ||
deleteMemberAccount(member); | ||
} | ||
|
||
private void sendWithdrawRequestToKakao(SocialType socialType, Member member) { | ||
RestTemplate restTemplate = new RestTemplate(); | ||
HttpHeaders headers = new HttpHeaders(); | ||
headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED); | ||
headers.add("Authorization", "KakaoAK " + kakaoAdminKey); | ||
String data = "target_id_type=user_id" + | ||
"&target_id=" + member.getOauthId(); | ||
HttpEntity <String> httpEntity = new HttpEntity<>(data, headers); | ||
ResponseEntity<String> response = restTemplate.exchange(kakaoWithdrawUri, HttpMethod.POST, httpEntity, String.class); | ||
} | ||
|
||
private void deleteMemberAccount(Member member) { | ||
memberRepository.delete(member); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
32 changes: 0 additions & 32 deletions
32
src/main/java/com/pyro/yolog/domain/member/query/AuthService.java
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.