From 8361dfdc74bbc230519b97eb223bc1ed60b195e5 Mon Sep 17 00:00:00 2001 From: Seokyeong Date: Mon, 5 Feb 2024 19:29:00 +0900 Subject: [PATCH 1/2] feat: social logout API --- .../com/fullcar/core/response/SuccessCode.java | 3 ++- .../application/auth/AuthServiceProvider.java | 6 ++++++ .../com/fullcar/member/domain/member/Member.java | 5 +++++ .../member/presentation/auth/AuthController.java | 14 ++++++++++++++ 4 files changed, 27 insertions(+), 1 deletion(-) diff --git a/src/main/java/com/fullcar/core/response/SuccessCode.java b/src/main/java/com/fullcar/core/response/SuccessCode.java index 1681ae5..e749b0f 100644 --- a/src/main/java/com/fullcar/core/response/SuccessCode.java +++ b/src/main/java/com/fullcar/core/response/SuccessCode.java @@ -15,7 +15,8 @@ public enum SuccessCode { SIGNIN_SUCCESS(OK, "소셜로그인 성공"), GET_NEW_TOKEN_SUCCESS(OK, "토큰 재발급 성공"), READ_SUCCESS(OK, "조회 성공"), - EMAIL_SENT_SUCCESS(OK, "인증메일 발송 성공"); + EMAIL_SENT_SUCCESS(OK, "인증메일 발송 성공"), + LOGOUT_SUCCESS(OK, "로그아웃 성공"); private final HttpStatus status; private final String message; diff --git a/src/main/java/com/fullcar/member/application/auth/AuthServiceProvider.java b/src/main/java/com/fullcar/member/application/auth/AuthServiceProvider.java index fc944f6..8f6abb5 100644 --- a/src/main/java/com/fullcar/member/application/auth/AuthServiceProvider.java +++ b/src/main/java/com/fullcar/member/application/auth/AuthServiceProvider.java @@ -13,6 +13,7 @@ import jakarta.annotation.PostConstruct; import lombok.RequiredArgsConstructor; import org.springframework.stereotype.Component; +import org.springframework.transaction.annotation.Transactional; import java.util.HashMap; import java.util.Map; @@ -63,4 +64,9 @@ public AuthTokenResponseDto getNewToken(String refreshToken) { .refreshToken(refreshToken) .build(); } + + @Transactional + public void socialLogout(Member member) { + member.clearRefreshTokenAndDeviceToken(); + } } diff --git a/src/main/java/com/fullcar/member/domain/member/Member.java b/src/main/java/com/fullcar/member/domain/member/Member.java index f8f5b7b..cee39ee 100644 --- a/src/main/java/com/fullcar/member/domain/member/Member.java +++ b/src/main/java/com/fullcar/member/domain/member/Member.java @@ -79,4 +79,9 @@ public void saveOnBoardingInfo(Member member) { this.gender = member.getGender(); this.company = new Company(member.getCompany().getCompanyName(), member.getCompany().getLatitude(), member.getCompany().getLongitude()); } + + public void clearRefreshTokenAndDeviceToken() { + this.deviceToken = null; + this.refreshToken = null; + } } diff --git a/src/main/java/com/fullcar/member/presentation/auth/AuthController.java b/src/main/java/com/fullcar/member/presentation/auth/AuthController.java index 706da7c..f509338 100644 --- a/src/main/java/com/fullcar/member/presentation/auth/AuthController.java +++ b/src/main/java/com/fullcar/member/presentation/auth/AuthController.java @@ -1,9 +1,11 @@ package com.fullcar.member.presentation.auth; +import com.fullcar.core.annotation.CurrentMember; import com.fullcar.core.response.ApiResponse; import com.fullcar.core.response.SuccessCode; import com.fullcar.member.application.auth.AuthService; import com.fullcar.member.application.auth.AuthServiceProvider; +import com.fullcar.member.domain.member.Member; import com.fullcar.member.presentation.auth.dto.request.AuthRequestDto; import com.fullcar.member.presentation.auth.dto.request.AuthTokenRequestDto; import com.fullcar.member.presentation.auth.dto.response.AuthResponseDto; @@ -53,4 +55,16 @@ public ApiResponse socialLogin(@RequestBody AuthRequestDto auth public ApiResponse getNewToken(@RequestBody AuthTokenRequestDto authTokenRequestDto) { return ApiResponse.success(SuccessCode.GET_NEW_TOKEN_SUCCESS, authServiceProvider.getNewToken(authTokenRequestDto.getRefreshToken())); } + + @Operation(summary = "로그아웃 API") + @ApiResponses(value = { + @io.swagger.v3.oas.annotations.responses.ApiResponse(responseCode = "200", description = "로그아웃 성공"), + @io.swagger.v3.oas.annotations.responses.ApiResponse(responseCode = "500", description = "서버 내부 오류", content = @Content) + }) + @PostMapping("/logout") + @ResponseStatus(HttpStatus.OK) + public ApiResponse socialLogout(@CurrentMember Member member) { + authServiceProvider.socialLogout(member); + return ApiResponse.success(SuccessCode.LOGOUT_SUCCESS); + } } From baab7e62f52878ab8a118376de64d581ad59527a Mon Sep 17 00:00:00 2001 From: Seokyeong Date: Tue, 6 Feb 2024 03:38:04 +0900 Subject: [PATCH 2/2] add: JPA persistence --- .../fullcar/member/application/auth/AuthServiceProvider.java | 5 ++++- src/main/java/com/fullcar/member/domain/member/Member.java | 2 +- 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/src/main/java/com/fullcar/member/application/auth/AuthServiceProvider.java b/src/main/java/com/fullcar/member/application/auth/AuthServiceProvider.java index 8f6abb5..3a3b347 100644 --- a/src/main/java/com/fullcar/member/application/auth/AuthServiceProvider.java +++ b/src/main/java/com/fullcar/member/application/auth/AuthServiceProvider.java @@ -11,6 +11,7 @@ import com.fullcar.member.presentation.auth.dto.response.AuthTokenResponseDto; import com.fullcar.member.presentation.auth.dto.response.SocialInfoResponseDto; import jakarta.annotation.PostConstruct; +import jakarta.persistence.EntityManager; import lombok.RequiredArgsConstructor; import org.springframework.stereotype.Component; import org.springframework.transaction.annotation.Transactional; @@ -27,6 +28,7 @@ public class AuthServiceProvider { private final AppleAuthService appleAuthService; private final JwtTokenProvider jwtTokenProvider; private final MemberRepository memberRepository; + private final EntityManager entityManager; @PostConstruct void initializeAuthServicesMap() { @@ -67,6 +69,7 @@ public AuthTokenResponseDto getNewToken(String refreshToken) { @Transactional public void socialLogout(Member member) { - member.clearRefreshTokenAndDeviceToken(); + memberRepository.findByIdAndIsDeletedOrThrow(member.getId(), false).clearRefreshTokenAndDeviceToken(); + memberRepository.flush(); } } diff --git a/src/main/java/com/fullcar/member/domain/member/Member.java b/src/main/java/com/fullcar/member/domain/member/Member.java index cee39ee..6aa2797 100644 --- a/src/main/java/com/fullcar/member/domain/member/Member.java +++ b/src/main/java/com/fullcar/member/domain/member/Member.java @@ -53,7 +53,7 @@ public class Member { @Column(name = "refresh_token") private String refreshToken; - @Column(name = "device_token", nullable = false) + @Column(name = "device_token") private String deviceToken; @Column(name = "created_at")