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

fix: separate saving device token API from social login API #96

Merged
merged 2 commits into from
Feb 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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
3 changes: 2 additions & 1 deletion src/main/java/com/fullcar/core/response/SuccessCode.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand All @@ -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)
Expand All @@ -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);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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();
}
}
9 changes: 7 additions & 2 deletions src/main/java/com/fullcar/member/domain/member/Member.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -74,9 +75,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;
}

Expand Down Expand Up @@ -108,4 +108,9 @@ public Member deleted() {
this.gender = null;
return this;
}

public Member saveDeviceToken(DeviceTokenRequestDto deviceTokenRequestDto) {
this.deviceToken = deviceTokenRequestDto.getDeviceToken();
return this;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,4 @@ public class KakaoAuthRequestDto {

@Schema(description = "μΉ΄μΉ΄μ˜€μ—μ„œ 받은 μ•‘μ„ΈμŠ€ 토큰", example = "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpYXQiOjE3MDUyMzQ2NjMsImV4cCI6MTcwNTIzNDg0MywibWVtYmVySWQiOiJjb20uZnVsbGNhci5tZW1iZXIuZG9tYWluLk1lbWJlcklkQDQ5ODRlODM3In0.Qf8uXvaWR9K_pD4jgZlnkFpcx2-zlRKENXJhGXkMGSs")
private String token;

@Schema(description = "λ””λ°”μ΄μŠ€ 토큰")
private String deviceToken;
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -84,4 +85,15 @@ public ApiResponse<Object> 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<DeviceTokenResponseDto> postDeviceToken(@CurrentMember Member member, @RequestBody DeviceTokenRequestDto deviceTokenRequestDto) {
DeviceTokenResponseDto responseDto = memberService.postDeviceToken(member, deviceTokenRequestDto);
return ApiResponse.success(SuccessCode.SAVE_DEVICE_TOKEN_SUCCESS, responseDto);
}
}
Original file line number Diff line number Diff line change
@@ -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;
}
Original file line number Diff line number Diff line change
@@ -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;
}
Loading