Skip to content

Commit

Permalink
๐Ÿ”ง apple Id Token์˜ email null์—ฌ๋ถ€์— ๋”ฐ๋ฅธ ์ •์ƒ ๊ฐ€์ž… ์ ˆ์ฐจ๋กœ ์ˆ˜์ •
Browse files Browse the repository at this point in the history
  • Loading branch information
youngreal committed Nov 20, 2024
1 parent 2a9f1e4 commit 9468aa1
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,8 @@

public interface WithdrawMemberRepository extends JpaRepository<WithdrawMember, Long> {
Optional<WithdrawMember> findByAppleId(String sub);

String findByEmail(String email);

boolean existsByAppleId(String sub);
}
50 changes: 38 additions & 12 deletions src/main/java/com/dnd/dndtravel/member/service/MemberService.java
Original file line number Diff line number Diff line change
@@ -1,15 +1,12 @@
package com.dnd.dndtravel.member.service;

import java.util.Optional;

import com.dnd.dndtravel.auth.repository.RefreshTokenRepository;
import com.dnd.dndtravel.map.exception.MemberNotFoundException;
import com.dnd.dndtravel.map.repository.MemberAttractionRepository;
import com.dnd.dndtravel.map.repository.MemberRegionRepository;
import com.dnd.dndtravel.auth.service.MemberNameGenerator;
import com.dnd.dndtravel.map.repository.WithdrawMemberRepository;
import com.dnd.dndtravel.member.domain.Member;
import com.dnd.dndtravel.member.domain.WithdrawMember;
import com.dnd.dndtravel.member.repository.MemberRepository;
import com.dnd.dndtravel.member.service.response.MyPageResponse;

Expand All @@ -30,18 +27,33 @@ public class MemberService {
private final WithdrawMemberRepository withDrawMemberRepository;
private final MemberNameGenerator memberNameGenerator;

/**
* 1. ํƒˆํ‡ดํ›„ ์žฌ๊ฐ€์ž… ์œ ์ €์˜ email์ด null์ธ๊ฒฝ์šฐ
* => withDraw ํ…Œ์ด๋ธ”์„ ์กฐํšŒํ›„ ์ตœ์ดˆ ๋กœ๊ทธ์ธ์‹œ ์ €์žฅํ–ˆ๋˜ sub๊ฐ’์œผ๋กœ ์œ ์ € ํŒ๋ณ„ํ›„ sub๊ฐ’์— ํ•ด๋‹นํ•˜๋Š” email ๊ฐ€์ ธ์™€ DB์— ์ €์žฅ
*
* 2. ํƒˆํ‡ดํ›„ ์žฌ๊ฐ€์ž… ์œ ์ €์˜ email์ด ์กด์žฌํ•˜๋Š”๊ฒฝ์šฐ
* => ํ•ด๋‹น email๋กœ withDraw ํ…Œ์ด๋ธ”์„ ์กฐํšŒํ›„, ํ•ด๋‹นํ•˜๋Š” email ๊ฐ€์ ธ์™€ DB์— ์ €์žฅ
*
* 3. ์ตœ์ดˆ ๋กœ๊ทธ์ธ ์œ ์ €์˜ email์ด null์ธ๊ฒฝ์šฐ
* => ๊ฐ€์ž… ์‹œ์ผœ์ค„์ˆ˜ ์—†์Œ (์˜ˆ์™ธ)
*
* 4. ์ตœ์ดˆ ๋กœ๊ทธ์ธ ์œ ์ €์˜ email์ด ์กด์žฌํ•˜๋Š”๊ฒฝ์šฐ
* => ํ•ด๋‹น email๋กœ withDraw ํ…Œ์ด๋ธ” ์กฐํšŒํ›„, email์กด์žฌ์—ฌ๋ถ€๋ฅผ ํ™•์ธํ•˜๊ณ  ์—†์œผ๋ฏ€๋กœ ์ž…๋ ฅ๋ฐ›์€ email์„ DB์— ์ €์žฅ
*/
@Transactional
public Member saveMember(String email, String sub, String selectedColor) {
// ์žฌ๊ฐ€์ž… ์œ ์ €
log.info("email = {}", email);
log.info("sub = {}", sub);
String targetEmail = Optional.ofNullable(email).orElseGet(() -> getWithdrawMemberEmail(sub));
if (isNewLoginMember(email)) {
withDrawMemberRepository.save(WithdrawMember.of(sub, email));
}

return memberRepository.findByEmail(targetEmail)
.orElseGet(() -> memberRepository.save(Member.of(memberNameGenerator.generateRandomName(), targetEmail,selectedColor)));
validateNewMemberEmail(email, sub);
String targetEmail = determineEmail(email, sub);
return memberRepository.save(
Member.of(
memberNameGenerator.generateRandomName(),
targetEmail,
selectedColor
)
);
}

@Transactional
Expand All @@ -68,7 +80,21 @@ private String getWithdrawMemberEmail(String sub) {
.getEmail();
}

private boolean isNewLoginMember(String email) {
return email != null;
private String determineEmail(String email, String sub) {
// ์ผ€์ด์Šค 1: ํƒˆํ‡ด ํ›„ ์žฌ๊ฐ€์ž… ์œ ์ € (์ด๋ฉ”์ผ ์—†์Œ)
if (email == null) {
return getWithdrawMemberEmail(sub);
}

// ์ผ€์ด์Šค 2, 4: ์ด๋ฉ”์ผ์ด ์žˆ๋Š” ๊ฒฝ์šฐ
String withdrawnEmail = withDrawMemberRepository.findByEmail(email);
return withdrawnEmail == null ? email : withdrawnEmail;
}

private void validateNewMemberEmail(String email, String sub) {
// ์ตœ๋กœ ๋กœ๊ทธ์ธ ์œ ์ €์ธ๋ฐ IdToken์˜ email๋„ null์ธ๊ฒฝ์šฐ (์ผ€์ด์Šค 3)
if (email == null && withDrawMemberRepository.existsByAppleId(sub)) {
throw new MemberNotFoundException(sub);
}
}
}

0 comments on commit 9468aa1

Please sign in to comment.