Skip to content

Commit

Permalink
KakaoId로 멤버를 조회할때 문제가 되었던 부분 수정 (#33)
Browse files Browse the repository at this point in the history
  • Loading branch information
soyesenna authored Nov 6, 2024
1 parent 8fa3e8d commit 2aa1442
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ public ResponseEntity<ApiData<OAuthLoginResponse>> getRedirect(
List<ResponseCookie> 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(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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<Member> 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())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<Member> findMemberByKakaoId(Long kakaoId) {
return this.memberRepository.findByKakaoId(kakaoId);
}
}

0 comments on commit 2aa1442

Please sign in to comment.