Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

KakaoId로 멤버를 조회할때 문제가 되었던 부분 수정 #33

Merged
merged 1 commit into from
Nov 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
}
}
Loading