From 2aa14426f7df3171ff4da2c96aaeb8c5879df8b2 Mon Sep 17 00:00:00 2001 From: soyesenna <85686722+soyesenna@users.noreply.github.com> Date: Wed, 6 Nov 2024 18:10:10 +0900 Subject: [PATCH] =?UTF-8?q?KakaoId=EB=A1=9C=20=EB=A9=A4=EB=B2=84=EB=A5=BC?= =?UTF-8?q?=20=EC=A1=B0=ED=9A=8C=ED=95=A0=EB=95=8C=20=EB=AC=B8=EC=A0=9C?= =?UTF-8?q?=EA=B0=80=20=EB=90=98=EC=97=88=EB=8D=98=20=EB=B6=80=EB=B6=84=20?= =?UTF-8?q?=EC=88=98=EC=A0=95=20(#33)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../auth/controller/AuthControllerImpl.java | 2 +- .../xclone/domain/auth/service/AuthService.java | 15 ++++++++++----- .../domain/member/service/MemberService.java | 7 +++---- 3 files changed, 14 insertions(+), 10 deletions(-) diff --git a/src/main/java/com/leets/team2/xclone/domain/auth/controller/AuthControllerImpl.java b/src/main/java/com/leets/team2/xclone/domain/auth/controller/AuthControllerImpl.java index 6a0d052..c01fc98 100644 --- a/src/main/java/com/leets/team2/xclone/domain/auth/controller/AuthControllerImpl.java +++ b/src/main/java/com/leets/team2/xclone/domain/auth/controller/AuthControllerImpl.java @@ -58,7 +58,7 @@ public ResponseEntity> getRedirect( List cookies = new ArrayList<>(); // 최초 로그인이 아니라면 바로 로그인 되도록 JWT 쿠키에 응답 if (!oAuthLoginResponse.requiredRegister()) { - JwtWrapper jwtWrapper = this.authService.generateJwt(kakaoInfo.properties().nickname(), kakaoInfo.id()); + JwtWrapper jwtWrapper = this.authService.generateJwt(kakaoInfo.id()); cookies.add(this.cookieUtils.addCookie(jwtWrapper.accessToken(), CookieSettings.ACCESS_TOKEN, CookieMaxAge.HALF_HOUR)); cookies.add( diff --git a/src/main/java/com/leets/team2/xclone/domain/auth/service/AuthService.java b/src/main/java/com/leets/team2/xclone/domain/auth/service/AuthService.java index d0b70c2..4f8c374 100644 --- a/src/main/java/com/leets/team2/xclone/domain/auth/service/AuthService.java +++ b/src/main/java/com/leets/team2/xclone/domain/auth/service/AuthService.java @@ -8,16 +8,20 @@ import com.leets.team2.xclone.domain.member.entities.Member; import com.leets.team2.xclone.domain.member.service.MemberService; import com.leets.team2.xclone.exception.AlreadyExistMemberException; +import com.leets.team2.xclone.exception.NoSuchMemberException; import com.leets.team2.xclone.utils.jwt.JwtUtils; import com.leets.team2.xclone.utils.jwt.JwtWrapper; import java.time.LocalDate; import java.time.LocalDateTime; import java.time.LocalTime; +import java.util.Optional; import lombok.RequiredArgsConstructor; +import lombok.extern.slf4j.Slf4j; import org.springframework.stereotype.Service; @Service @RequiredArgsConstructor +@Slf4j public class AuthService { private final MemberService memberService; @@ -32,16 +36,17 @@ public OAuthLoginResponse oauthLogin(KakaoInfo kakaoInfo) { String kakaoNickname = kakaoInfo.properties().nickname(); Long kakaoId = kakaoInfo.id(); - boolean isExistMember = this.memberService.checkMemberExistsByKakaoId(kakaoId); + Optional foundMember = this.memberService.findMemberByKakaoId(kakaoId); return new OAuthLoginResponse( - !isExistMember, - isExistMember ? this.memberService.findMemberByKakaoId(kakaoId).getNickname() : kakaoNickname + foundMember.isEmpty(), + foundMember.isEmpty() ? kakaoNickname : foundMember.get().getNickname() ); } - public JwtWrapper generateJwt(String nickname, Long kakaoId) { - Member member = this.memberService.findMemberBy(nickname, kakaoId); + public JwtWrapper generateJwt(Long kakaoId) { + Member member = this.memberService.findMemberByKakaoId(kakaoId).orElseThrow( + NoSuchMemberException::new); return new JwtWrapper( this.jwtUtils.generateAccessToken(member.getTag()), this.jwtUtils.generateRefreshToken(member.getTag()) diff --git a/src/main/java/com/leets/team2/xclone/domain/member/service/MemberService.java b/src/main/java/com/leets/team2/xclone/domain/member/service/MemberService.java index 5acf66f..714f4a5 100644 --- a/src/main/java/com/leets/team2/xclone/domain/member/service/MemberService.java +++ b/src/main/java/com/leets/team2/xclone/domain/member/service/MemberService.java @@ -3,6 +3,7 @@ import com.leets.team2.xclone.domain.member.entities.Member; import com.leets.team2.xclone.domain.member.repository.MemberRepository; import com.leets.team2.xclone.exception.NoSuchMemberException; +import java.util.Optional; import lombok.RequiredArgsConstructor; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; @@ -36,9 +37,7 @@ public boolean checkMemberExistsByKakaoId(Long kakaoId) { return this.memberRepository.existsByKakaoId(kakaoId); } - public Member findMemberByKakaoId(Long kakaoId) { - return this.memberRepository.findByKakaoId(kakaoId).orElseThrow( - NoSuchMemberException::new - ); + public Optional findMemberByKakaoId(Long kakaoId) { + return this.memberRepository.findByKakaoId(kakaoId); } }