From 83b40dbe00c4241b26eaae37141f32f4cdf7a574 Mon Sep 17 00:00:00 2001 From: yeon015 Date: Sat, 18 Jan 2025 01:04:38 +0900 Subject: [PATCH] =?UTF-8?q?refactor(#280)=20:=20=EC=95=BD=EA=B4=80=20?= =?UTF-8?q?=EB=8F=99=EC=9D=98=20=EA=B4=80=EB=A0=A8=20=EC=B9=B4=EC=B9=B4?= =?UTF-8?q?=EC=98=A4=20=EB=A1=9C=EA=B7=B8=EC=9D=B8=20&=20=ED=9A=8C?= =?UTF-8?q?=EC=9B=90=EA=B0=80=EC=9E=85=20version2=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../juinjang/controller/OAuthController.java | 27 ++++++ .../kakao/KakaoSignUpRequestVersion2Dto.java | 19 ++++ .../juinjang/service/auth/OAuthService.java | 92 +++++++++++++++++++ 3 files changed, 138 insertions(+) create mode 100644 src/main/java/umc/th/juinjang/model/dto/auth/kakao/KakaoSignUpRequestVersion2Dto.java diff --git a/src/main/java/umc/th/juinjang/controller/OAuthController.java b/src/main/java/umc/th/juinjang/controller/OAuthController.java index 22fe8c9..108ed1a 100644 --- a/src/main/java/umc/th/juinjang/controller/OAuthController.java +++ b/src/main/java/umc/th/juinjang/controller/OAuthController.java @@ -23,6 +23,7 @@ import umc.th.juinjang.model.dto.auth.apple.AppleSignUpRequestVersion2Dto; import umc.th.juinjang.model.dto.auth.kakao.KakaoLoginRequestDto; import umc.th.juinjang.model.dto.auth.kakao.KakaoSignUpRequestDto; +import umc.th.juinjang.model.dto.auth.kakao.KakaoSignUpRequestVersion2Dto; import umc.th.juinjang.model.entity.Member; import umc.th.juinjang.model.entity.Withdraw; import umc.th.juinjang.repository.withdraw.WithdrawRepository; @@ -70,6 +71,32 @@ public ApiResponse kakaoSignUp(@RequestHeader("target-id") Str return ApiResponse.onSuccess(oauthService.kakaoSignUp(targetId, kakaoSignUpReqDto)); } + //V2 + // 카카오 로그인 + @PostMapping("/v2/kakao/login") + public ApiResponse kakaoLoginVersion2(@RequestHeader("target-id") String kakaoTargetId, @RequestBody @Validated KakaoLoginRequestDto kakaoReqDto) { + Long targetId; + if(kakaoTargetId == null) { + throw new ExceptionHandler(EMPTY_TARGET_ID); + } + + targetId = Long.parseLong(kakaoTargetId); + return ApiResponse.onSuccess(oauthService.kakaoLoginVersion2(targetId, kakaoReqDto)); + } + + // 카카오 로그인 (회원가입) + @PostMapping("/v2/kakao/signup") + public ApiResponse kakaoSignUpVersion2(@RequestHeader("target-id") String kakaoTargetId, @RequestBody @Validated KakaoSignUpRequestVersion2Dto kakaoSignUpReqDto) { + Long targetId; + if(kakaoTargetId == null) { + throw new ExceptionHandler(EMPTY_TARGET_ID); + } + + targetId = Long.parseLong(kakaoTargetId); + return ApiResponse.onSuccess(oauthService.kakaoSignUpVersion2(targetId, kakaoSignUpReqDto)); + } + + // refreshToken으로 accessToken 재발급 // Authorization : Bearer Token에 refreshToken 담기 @PostMapping("/regenerate-token") diff --git a/src/main/java/umc/th/juinjang/model/dto/auth/kakao/KakaoSignUpRequestVersion2Dto.java b/src/main/java/umc/th/juinjang/model/dto/auth/kakao/KakaoSignUpRequestVersion2Dto.java new file mode 100644 index 0000000..a3fe305 --- /dev/null +++ b/src/main/java/umc/th/juinjang/model/dto/auth/kakao/KakaoSignUpRequestVersion2Dto.java @@ -0,0 +1,19 @@ +package umc.th.juinjang.model.dto.auth.kakao; + +import jakarta.validation.constraints.NotEmpty; +import lombok.AccessLevel; +import lombok.Getter; +import lombok.NoArgsConstructor; + +@Getter +@NoArgsConstructor(access = AccessLevel.PROTECTED) +public class KakaoSignUpRequestVersion2Dto { + + @NotEmpty + private String email; + private String kakaoNickname; + @NotEmpty + private String nickname; + @NotEmpty + private String agreeVersion; +} diff --git a/src/main/java/umc/th/juinjang/service/auth/OAuthService.java b/src/main/java/umc/th/juinjang/service/auth/OAuthService.java index 48cc3e8..739e0cb 100644 --- a/src/main/java/umc/th/juinjang/service/auth/OAuthService.java +++ b/src/main/java/umc/th/juinjang/service/auth/OAuthService.java @@ -17,6 +17,7 @@ import umc.th.juinjang.model.dto.auth.apple.*; import umc.th.juinjang.model.dto.auth.kakao.KakaoLoginRequestDto; import umc.th.juinjang.model.dto.auth.kakao.KakaoSignUpRequestDto; +import umc.th.juinjang.model.dto.auth.kakao.KakaoSignUpRequestVersion2Dto; import umc.th.juinjang.model.entity.Image; import umc.th.juinjang.model.entity.Limjang; import umc.th.juinjang.model.entity.Member; @@ -407,6 +408,97 @@ public void deleteFromS3(List urlList){ } } + // V2 + // 카카오 + @Transactional + public LoginResponseVersion2Dto kakaoLoginVersion2(Long targetId, KakaoLoginRequestDto kakaoReqDto) { + String email = kakaoReqDto.getEmail(); + log.info(kakaoReqDto.getEmail()); + + if(email == null) + throw new MemberHandler(MEMBER_EMAIL_NOT_FOUND); + + Optional getMemberByEmail = memberRepository.findByEmail(email); + Optional getMemberByTargetId = memberRepository.findByKakaoTargetId(targetId); + Member member = null; + + + if(getMemberByEmail.isPresent() && getMemberByTargetId.isEmpty()){ + if(!getMemberByEmail.get().getProvider().equals(MemberProvider.KAKAO)) { // 이미 회원가입했지만 Kakao가 아닌 다른 소셜 로그인 사용 + throw new MemberHandler(MEMBER_NOT_FOUND_IN_KAKAO); + } else { // 잘못된 target_id가 들어왔을때(db에 없는) + throw new MemberHandler(UNCORRECTED_TARGET_ID); + } + } else if(getMemberByEmail.isPresent() && getMemberByTargetId.isPresent()){ // 이미 회원가입한 회원인 경우 + if(!getMemberByEmail.get().getProvider().equals(MemberProvider.KAKAO)) { // 이미 회원가입했지만 Kakao가 아닌 다른 소셜 로그인 사용 + throw new MemberHandler(MEMBER_NOT_FOUND_IN_KAKAO); + } else if(getMemberByEmail.get().getMemberId() != getMemberByTargetId.get().getMemberId()) { + throw new MemberHandler(FAILED_TO_LOGIN); + } + member = getMemberByEmail.get(); + } else if(getMemberByEmail.isEmpty() && getMemberByTargetId.isEmpty()){ // 회원가입이 안되어있는 경우 -> 에러 발생. 회원가입 해야 함 + throw new MemberHandler(MEMBER_NOT_FOUND); + } + + if(member == null) { + throw new MemberHandler(FAILED_TO_LOGIN); + } + + // accessToken, refreshToken 발급 후 반환 + return createTokenVersion2(member); + } + + // 카카오 로그인 (회원가입 해야하는 경우) + @Transactional + public LoginResponseVersion2Dto kakaoSignUpVersion2(Long targetId, KakaoSignUpRequestVersion2Dto kakaoSignUpReqDto) { + String email = kakaoSignUpReqDto.getEmail(); + log.info(kakaoSignUpReqDto.getEmail()); + + if(email == null) + throw new MemberHandler(MEMBER_EMAIL_NOT_FOUND); + + Optional getMember = memberRepository.findByEmail(email); + Optional getTargetId = memberRepository.findByKakaoTargetId(targetId); + + Member member = null; + + if(getMember.isPresent() && getTargetId.isEmpty() && getMember.get().getProvider().equals(MemberProvider.APPLE)) { + throw new MemberHandler(MEMBER_NOT_FOUND_IN_KAKAO); + } else if(getMember.isPresent() && getTargetId.isPresent()) { +// if(!getMember.get().getProvider().equals(MemberProvider.KAKAO)) { // 이미 회원가입했지만 Kakao가 아닌 다른 소셜 로그인 사용 +// throw new MemberHandler(MEMBER_NOT_FOUND_IN_KAKAO); +// } else + if((getTargetId.get().getMemberId() != getMember.get().getMemberId())) { + throw new MemberHandler(FAILED_TO_LOGIN); + } else if(getMember.get().getProvider().equals(MemberProvider.KAKAO)) { + throw new MemberHandler(ALREADY_MEMBER); + } + } else if (getMember.isPresent() || getTargetId.isPresent()) { // 둘 중 하나만 존재할 때 실행될 코드 + throw new MemberHandler(FAILED_TO_LOGIN); + } else if (!getMember.isPresent() && !getTargetId.isPresent()) { // 두 값 모두 존재하지 않을 때 실행될 코드, 아직 회원가입 하지 않은 회원인 경우 + member = memberRepository.save( + Member.builder() + .email(email) + .provider(MemberProvider.KAKAO) + .kakaoTargetId(targetId) + .nickname(kakaoSignUpReqDto.getNickname()) + .refreshToken("") + .refreshTokenExpiresAt(LocalDateTime.now()) + .agreeVersion(kakaoSignUpReqDto.getAgreeVersion()) + .build() + ); + } + + if(member == null) { + throw new MemberHandler(FAILED_TO_SIGNUP); + } + + // accessToken, refreshToken 발급 후 반환 + publishDiscordAlert(member); + return createTokenVersion2(member); + } + + // 애플 public LoginResponseVersion2Dto appleLoginVersion2(AppleLoginRequestDto appleLoginRequest) { // email, sub값 추출 후 db에서 해당 email값 그리고 sub값을 가진 유저가 있는지 find // 1. 추출한 email, sub 값이 null이면 -> 잘못된 apple token