From f2a56a968cbe762a703bc59b2f0647e54afc07a7 Mon Sep 17 00:00:00 2001 From: Seokyeong Date: Mon, 19 Feb 2024 21:45:14 +0900 Subject: [PATCH 1/2] del: device token from social login API --- .../member/application/auth/AppleAuthService.java | 9 ++++----- .../member/application/auth/KakaoAuthService.java | 9 ++++----- .../fullcar/member/application/member/MemberMapper.java | 6 ++---- .../java/com/fullcar/member/domain/member/Member.java | 3 +-- .../auth/dto/request/AppleAuthRequestDto.java | 3 --- .../auth/dto/request/KakaoAuthRequestDto.java | 3 --- 6 files changed, 11 insertions(+), 22 deletions(-) diff --git a/src/main/java/com/fullcar/member/application/auth/AppleAuthService.java b/src/main/java/com/fullcar/member/application/auth/AppleAuthService.java index c0d01bd..bcb153a 100644 --- a/src/main/java/com/fullcar/member/application/auth/AppleAuthService.java +++ b/src/main/java/com/fullcar/member/application/auth/AppleAuthService.java @@ -66,7 +66,6 @@ public class AppleAuthService { @Transactional public SocialInfoResponseDto getMemberInfo(AppleAuthRequestDto appleAuthRequestDto) throws IOException { - String deviceToken = appleAuthRequestDto.getDeviceToken(); String idToken = appleAuthRequestDto.getIdToken(); String appleRefreshToken = requestAppleAuthToken(appleAuthRequestDto.getAuthCode()).getRefreshToken(); @@ -84,9 +83,9 @@ public SocialInfoResponseDto getMemberInfo(AppleAuthRequestDto appleAuthRequestD if (memberRepository.existsBySocialId(socialId)) { Member member = memberRepository.findBySocialId(socialId); member.saveAppleRefreshToken(appleRefreshToken); - member.loginMember(deviceToken, refreshToken); + member.loginMember(refreshToken); } - else createMember(socialId, appleRefreshToken, deviceToken, refreshToken); + else createMember(socialId, appleRefreshToken, refreshToken); return SocialInfoResponseDto.builder() .socialId(socialId) @@ -95,8 +94,8 @@ public SocialInfoResponseDto getMemberInfo(AppleAuthRequestDto appleAuthRequestD } // 새로운 멤버 생성 - private void createMember(SocialId socialId, String authCode, String deviceToken, String refreshToken) { - Member member = memberMapper.toAppleLoginEntity(socialId, authCode, deviceToken, refreshToken); + private void createMember(SocialId socialId, String authCode, String refreshToken) { + Member member = memberMapper.toAppleLoginEntity(socialId, authCode, refreshToken); memberRepository.saveAndFlush(member); } diff --git a/src/main/java/com/fullcar/member/application/auth/KakaoAuthService.java b/src/main/java/com/fullcar/member/application/auth/KakaoAuthService.java index afc9ec6..4948be9 100644 --- a/src/main/java/com/fullcar/member/application/auth/KakaoAuthService.java +++ b/src/main/java/com/fullcar/member/application/auth/KakaoAuthService.java @@ -42,12 +42,11 @@ public class KakaoAuthService { @Transactional public SocialInfoResponseDto getMemberInfo(KakaoAuthRequestDto kakaoAuthRequestDto) { - String deviceToken = kakaoAuthRequestDto.getDeviceToken(); SocialId socialId = socialIdService.generateSocialId(getKakaoData(kakaoAuthRequestDto.getToken())); String refreshToken = jwtTokenProvider.generateRefreshToken(); - if (memberRepository.existsBySocialId(socialId)) memberRepository.findBySocialId(socialId).loginMember(deviceToken, refreshToken); - else createMember(socialId, deviceToken, refreshToken); + if (memberRepository.existsBySocialId(socialId)) memberRepository.findBySocialId(socialId).loginMember(refreshToken); + else createMember(socialId, refreshToken); return SocialInfoResponseDto.builder() .socialId(socialId) @@ -87,8 +86,8 @@ private static String getKakaoData(String kakaoToken) { } // 새로운 멤버 생성 - private void createMember(SocialId socialId, String deviceToken, String refreshToken) { - Member member = memberMapper.toKakaoLoginEntity(socialId, deviceToken, refreshToken); + private void createMember(SocialId socialId, String refreshToken) { + Member member = memberMapper.toKakaoLoginEntity(socialId, refreshToken); memberRepository.saveAndFlush(member); } diff --git a/src/main/java/com/fullcar/member/application/member/MemberMapper.java b/src/main/java/com/fullcar/member/application/member/MemberMapper.java index 5f698f5..e2be869 100644 --- a/src/main/java/com/fullcar/member/application/member/MemberMapper.java +++ b/src/main/java/com/fullcar/member/application/member/MemberMapper.java @@ -54,22 +54,20 @@ public Member toEntity(OnBoardingRequestDto onboardingRequestDto) { .build(); } - public Member toKakaoLoginEntity(SocialId socialId, String deviceToken, String refreshToken) { + public Member toKakaoLoginEntity(SocialId socialId, String refreshToken) { return Member.builder() .id(memberIdService.nextId()) .socialId(socialId) - .deviceToken(deviceToken) .refreshToken(refreshToken) .socialType(SocialType.KAKAO) .build(); } - public Member toAppleLoginEntity(SocialId socialId, String appleRefreshToken, String deviceToken, String refreshToken) { + public Member toAppleLoginEntity(SocialId socialId, String appleRefreshToken, String refreshToken) { return Member.builder() .id(memberIdService.nextId()) .socialId(socialId) .appleRefreshToken(appleRefreshToken) - .deviceToken(deviceToken) .refreshToken(refreshToken) .socialType(SocialType.APPLE) .build(); 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 57476ea..eca9466 100644 --- a/src/main/java/com/fullcar/member/domain/member/Member.java +++ b/src/main/java/com/fullcar/member/domain/member/Member.java @@ -74,9 +74,8 @@ public void saveAppleRefreshToken(String appleRefreshToken) { this.appleRefreshToken = appleRefreshToken; } - public void loginMember(String deviceToken, String refreshToken) { + public void loginMember(String refreshToken) { this.isDeleted = false; - this.deviceToken = deviceToken; this.refreshToken = refreshToken; } diff --git a/src/main/java/com/fullcar/member/presentation/auth/dto/request/AppleAuthRequestDto.java b/src/main/java/com/fullcar/member/presentation/auth/dto/request/AppleAuthRequestDto.java index 73c2085..d5101f6 100644 --- a/src/main/java/com/fullcar/member/presentation/auth/dto/request/AppleAuthRequestDto.java +++ b/src/main/java/com/fullcar/member/presentation/auth/dto/request/AppleAuthRequestDto.java @@ -16,7 +16,4 @@ public class AppleAuthRequestDto { @Schema(description = "id_token", example = "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpYXQiOjE3MDUyMzQ2NjMsImV4cCI6MTcwNTIzNDg0MywibWVtYmVySWQiOiJjb20uZnVsbGNhci5tZW1iZXIuZG9tYWluLk1lbWJlcklkQDQ5ODRlODM3In0.Qf8uXvaWR9K_pD4jgZlnkFpcx2-zlRKENXJhGXkMGSs") private String idToken; - - @Schema(description = "디바이스 토큰") - private String deviceToken; } diff --git a/src/main/java/com/fullcar/member/presentation/auth/dto/request/KakaoAuthRequestDto.java b/src/main/java/com/fullcar/member/presentation/auth/dto/request/KakaoAuthRequestDto.java index 8232073..e65a8f3 100644 --- a/src/main/java/com/fullcar/member/presentation/auth/dto/request/KakaoAuthRequestDto.java +++ b/src/main/java/com/fullcar/member/presentation/auth/dto/request/KakaoAuthRequestDto.java @@ -14,7 +14,4 @@ public class KakaoAuthRequestDto { @Schema(description = "카카오에서 받은 액세스 토큰", example = "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpYXQiOjE3MDUyMzQ2NjMsImV4cCI6MTcwNTIzNDg0MywibWVtYmVySWQiOiJjb20uZnVsbGNhci5tZW1iZXIuZG9tYWluLk1lbWJlcklkQDQ5ODRlODM3In0.Qf8uXvaWR9K_pD4jgZlnkFpcx2-zlRKENXJhGXkMGSs") private String token; - - @Schema(description = "디바이스 토큰") - private String deviceToken; } From dec12568436b35543987a86206042dc4036ffb84 Mon Sep 17 00:00:00 2001 From: Seokyeong Date: Mon, 19 Feb 2024 22:00:29 +0900 Subject: [PATCH 2/2] feat: save device token API --- .../com/fullcar/core/response/SuccessCode.java | 3 ++- .../member/application/member/MemberService.java | 15 +++++++++++---- .../com/fullcar/member/domain/member/Member.java | 6 ++++++ .../presentation/member/MemberController.java | 12 ++++++++++++ .../member/dto/request/DeviceTokenRequestDto.java | 15 +++++++++++++++ .../dto/response/DeviceTokenResponseDto.java | 13 +++++++++++++ 6 files changed, 59 insertions(+), 5 deletions(-) create mode 100644 src/main/java/com/fullcar/member/presentation/member/dto/request/DeviceTokenRequestDto.java create mode 100644 src/main/java/com/fullcar/member/presentation/member/dto/response/DeviceTokenResponseDto.java diff --git a/src/main/java/com/fullcar/core/response/SuccessCode.java b/src/main/java/com/fullcar/core/response/SuccessCode.java index fc6f1c4..61cc47e 100644 --- a/src/main/java/com/fullcar/core/response/SuccessCode.java +++ b/src/main/java/com/fullcar/core/response/SuccessCode.java @@ -21,7 +21,8 @@ public enum SuccessCode { AVAILABLE_NICKNAME(OK, "사용 가능한 닉네임"), UPDATE_SUCCESS(OK, "수정 성공"), CODE_VERIFICATION_SUCCESS(OK, "인증 성공"), - WITHDRAW_SUCCESS(OK, "탈퇴 성공"); + WITHDRAW_SUCCESS(OK, "탈퇴 성공"), + SAVE_DEVICE_TOKEN_SUCCESS(OK, "디바이스 토큰 등록 성공"); private final HttpStatus status; private final String message; diff --git a/src/main/java/com/fullcar/member/application/member/MemberService.java b/src/main/java/com/fullcar/member/application/member/MemberService.java index e964864..8684983 100644 --- a/src/main/java/com/fullcar/member/application/member/MemberService.java +++ b/src/main/java/com/fullcar/member/application/member/MemberService.java @@ -3,13 +3,11 @@ import com.fullcar.core.exception.CustomException; import com.fullcar.core.exception.NotFoundException; import com.fullcar.core.response.ErrorCode; -import com.fullcar.member.application.auth.AppleAuthService; -import com.fullcar.member.application.auth.KakaoAuthService; -import com.fullcar.member.domain.car.CarRepository; -import com.fullcar.member.domain.mail.MailRepository; import com.fullcar.member.domain.member.*; +import com.fullcar.member.presentation.member.dto.request.DeviceTokenRequestDto; import com.fullcar.member.presentation.member.dto.request.NicknameRequestDto; import com.fullcar.member.presentation.member.dto.request.OnBoardingRequestDto; +import com.fullcar.member.presentation.member.dto.response.DeviceTokenResponseDto; import com.fullcar.member.presentation.member.dto.response.MemberGetResponseDto; import com.fullcar.member.presentation.member.dto.response.NicknameResponseDto; import com.fullcar.member.presentation.member.dto.response.OnBoardingResponseDto; @@ -59,4 +57,13 @@ public NicknameResponseDto checkNicknameDuplication(NicknameRequestDto nicknameR .build(); } } + + @Transactional + public DeviceTokenResponseDto postDeviceToken(Member member, DeviceTokenRequestDto deviceTokenRequestDto) { + memberRepository.saveAndFlush(member.saveDeviceToken(deviceTokenRequestDto)); + + return DeviceTokenResponseDto.builder() + .deviceToken(deviceTokenRequestDto.getDeviceToken()) + .build(); + } } 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 eca9466..a935330 100644 --- a/src/main/java/com/fullcar/member/domain/member/Member.java +++ b/src/main/java/com/fullcar/member/domain/member/Member.java @@ -2,6 +2,7 @@ import com.fullcar.member.domain.car.CarId; import com.fullcar.member.domain.auth.SocialId; +import com.fullcar.member.presentation.member.dto.request.DeviceTokenRequestDto; import jakarta.persistence.*; import lombok.AllArgsConstructor; import lombok.Builder; @@ -107,4 +108,9 @@ public Member deleted() { this.gender = null; return this; } + + public Member saveDeviceToken(DeviceTokenRequestDto deviceTokenRequestDto) { + this.deviceToken = deviceTokenRequestDto.getDeviceToken(); + return this; + } } diff --git a/src/main/java/com/fullcar/member/presentation/member/MemberController.java b/src/main/java/com/fullcar/member/presentation/member/MemberController.java index 6643d68..d6e88b4 100644 --- a/src/main/java/com/fullcar/member/presentation/member/MemberController.java +++ b/src/main/java/com/fullcar/member/presentation/member/MemberController.java @@ -7,6 +7,7 @@ import com.fullcar.member.domain.member.Member; import com.fullcar.member.domain.member.service.MailService; import com.fullcar.member.presentation.member.dto.request.*; +import com.fullcar.member.presentation.member.dto.response.DeviceTokenResponseDto; import com.fullcar.member.presentation.member.dto.response.MemberGetResponseDto; import com.fullcar.member.presentation.member.dto.response.NicknameResponseDto; import com.fullcar.member.presentation.member.dto.response.OnBoardingResponseDto; @@ -84,4 +85,15 @@ public ApiResponse checkMailAuthenticationCode(@CurrentMember Member mem mailService.checkMailAuthenticationCode(member, codeRequestDto); return ApiResponse.success(SuccessCode.CODE_VERIFICATION_SUCCESS); } + + @Operation(summary = "디바이스 토큰 등록 API") + @ApiResponses({ + @io.swagger.v3.oas.annotations.responses.ApiResponse(responseCode = "200", description = "디바이스 토큰 등록 성공"), + @io.swagger.v3.oas.annotations.responses.ApiResponse(responseCode = "500", description = "서버 내부 오류", content = @Content) + }) + @PostMapping("/device-token") + public ApiResponse postDeviceToken(@CurrentMember Member member, @RequestBody DeviceTokenRequestDto deviceTokenRequestDto) { + DeviceTokenResponseDto responseDto = memberService.postDeviceToken(member, deviceTokenRequestDto); + return ApiResponse.success(SuccessCode.SAVE_DEVICE_TOKEN_SUCCESS, responseDto); + } } diff --git a/src/main/java/com/fullcar/member/presentation/member/dto/request/DeviceTokenRequestDto.java b/src/main/java/com/fullcar/member/presentation/member/dto/request/DeviceTokenRequestDto.java new file mode 100644 index 0000000..33d3eb4 --- /dev/null +++ b/src/main/java/com/fullcar/member/presentation/member/dto/request/DeviceTokenRequestDto.java @@ -0,0 +1,15 @@ +package com.fullcar.member.presentation.member.dto.request; + +import io.swagger.v3.oas.annotations.media.Schema; +import lombok.AccessLevel; +import lombok.AllArgsConstructor; +import lombok.Getter; +import lombok.NoArgsConstructor; + +@Getter +@AllArgsConstructor(access = AccessLevel.PROTECTED) +@NoArgsConstructor(access = AccessLevel.PROTECTED) +public class DeviceTokenRequestDto { + @Schema(description = "디바이스 토큰", example = "123") + private String deviceToken; +} diff --git a/src/main/java/com/fullcar/member/presentation/member/dto/response/DeviceTokenResponseDto.java b/src/main/java/com/fullcar/member/presentation/member/dto/response/DeviceTokenResponseDto.java new file mode 100644 index 0000000..3fc0141 --- /dev/null +++ b/src/main/java/com/fullcar/member/presentation/member/dto/response/DeviceTokenResponseDto.java @@ -0,0 +1,13 @@ +package com.fullcar.member.presentation.member.dto.response; + +import io.swagger.v3.oas.annotations.media.Schema; +import lombok.*; + +@Getter +@Builder +@AllArgsConstructor(access = AccessLevel.PROTECTED) +@NoArgsConstructor(access = AccessLevel.PROTECTED) +public class DeviceTokenResponseDto { + @Schema(description = "디바이스 토큰", example = "123") + private String deviceToken; +}