Skip to content

Commit

Permalink
refactor(#280) : 약관 동의 관련 카카오 로그인 & 회원가입 version2 추가
Browse files Browse the repository at this point in the history
  • Loading branch information
yeon015 committed Jan 17, 2025
1 parent 89b46ba commit 83b40db
Show file tree
Hide file tree
Showing 3 changed files with 138 additions and 0 deletions.
27 changes: 27 additions & 0 deletions src/main/java/umc/th/juinjang/controller/OAuthController.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -70,6 +71,32 @@ public ApiResponse<LoginResponseDto> kakaoSignUp(@RequestHeader("target-id") Str
return ApiResponse.onSuccess(oauthService.kakaoSignUp(targetId, kakaoSignUpReqDto));
}

//V2
// 카카오 로그인
@PostMapping("/v2/kakao/login")
public ApiResponse<LoginResponseVersion2Dto> 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<LoginResponseVersion2Dto> 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")
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
}
92 changes: 92 additions & 0 deletions src/main/java/umc/th/juinjang/service/auth/OAuthService.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -407,6 +408,97 @@ public void deleteFromS3(List<String> 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<Member> getMemberByEmail = memberRepository.findByEmail(email);
Optional<Member> 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<Member> getMember = memberRepository.findByEmail(email);
Optional<Member> 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
Expand Down

0 comments on commit 83b40db

Please sign in to comment.