Skip to content

Commit

Permalink
♻️ refactor: SDK로 받은 토큰으로 인증하기 구현 (#85)
Browse files Browse the repository at this point in the history
♻️ refactor: SDK로 받은 토큰으로 인증하기 구현 (#85)
  • Loading branch information
jiiiiiw authored Aug 21, 2024
2 parents 914d644 + 92eaa03 commit a02a06a
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@
import com.example.template.domain.member.dto.MemberResponseDTO;
import com.example.template.domain.member.dto.SocialRequestDTO;
import com.example.template.domain.member.entity.Member;
import com.example.template.domain.member.exception.MemberErrorCode;
import com.example.template.domain.member.exception.MemberException;
import com.example.template.domain.member.jwt.dto.JwtDTO;
import com.example.template.domain.member.jwt.exception.SecurityCustomException;
import com.example.template.domain.member.service.KakaoService;
import com.example.template.domain.member.service.MemberService;
import com.example.template.global.annotation.AuthenticatedMember;
Expand Down Expand Up @@ -47,6 +50,22 @@ public ApiResponse<MemberResponseDTO.LoginResultDTO> loginOrSignupByKakao(@Valid
return ApiResponse.onSuccess(kakaoService.loginOrSignupByKakao(requestDTO));
}

@Operation(summary = "카카오 로그인 및 회원가입_SDK", description = "카카오 accessToken을 헤더로 받아 로그인 또는 회원가입을 처리합니다. " +
"반환 값으로 JWT accessToken과 refreshToken이 발급되며, accessToken 값을 Authorize에 인증")
@PostMapping("/social/oauth/kakao")
public ApiResponse<MemberResponseDTO.LoginResultDTO> androidKakao(
@RequestHeader("Authorization") String authorizationHeader) {

// Authorization 헤더에서 Bearer 토큰 추출
if (authorizationHeader != null) {
String accessToken = authorizationHeader;
// 카카오 서비스 호출하여 로그인 또는 회원가입 처리
return ApiResponse.onSuccess(kakaoService.androidKakao(accessToken));
} else {
throw new MemberException(MemberErrorCode._INTERNAL_SERVER_ERROR);
}
}

@Operation(summary = "로그아웃")
@PostMapping("/logout")
public ApiResponse<String> logout(HttpServletRequest request) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,6 @@ public interface KakaoService {
void kakaoUnlink(String accessToken);

MemberResponseDTO.LoginResultDTO loginOrSignupByKakao(SocialRequestDTO.LoginDTO requestDTO);

MemberResponseDTO.LoginResultDTO androidKakao(String accessToken);
}
Original file line number Diff line number Diff line change
Expand Up @@ -154,4 +154,60 @@ public MemberResponseDTO.LoginResultDTO loginOrSignupByKakao(SocialRequestDTO.Lo
.build();
});
}

@Override
public MemberResponseDTO.LoginResultDTO androidKakao(String accessToken) {
// 카카오 프로필 가져오기
KakaoProfile kakaoProfile = getKakaoProfile(accessToken);

if (kakaoProfile == null) {
throw new MemberException(MemberErrorCode.MEMBER_NOT_FOUND);
}

String kakaoEmail = kakaoProfile.getKakao_account().getEmail();
if (kakaoEmail == null) {
kakaoUnlink(accessToken);
throw new MemberException(MemberErrorCode.EMAIL_NOT_EXIST);
}

// 회원 정보 존재 여부 확인
return memberRepository.findByEmailAndProvider(kakaoEmail, ProviderType.KAKAO)
.map(member -> {
// 로그인 처리
PrincipalDetails userDetails = new PrincipalDetails(member);
String jwtAccessToken = jwtProvider.createJwtAccessToken(userDetails);
String jwtRefreshToken = jwtProvider.createJwtRefreshToken(userDetails);

return MemberResponseDTO.LoginResultDTO.builder()
.userId(member.getId())
.createdAt(LocalDateTime.now())
.accessToken(jwtAccessToken)
.refreshToken(jwtRefreshToken)
.build();
})
.orElseGet(() -> {
// 회원가입 처리 후 로그인 처리
MemberRequestDTO.SignupDTO signupRequestDto = MemberRequestDTO.SignupDTO.builder()
.email(kakaoEmail)
.name(kakaoProfile.getProperties().getNickname())
.provider(ProviderType.KAKAO)
.build();

memberService.socialSignup(signupRequestDto);

Member member = memberRepository.findByEmailAndProvider(kakaoEmail, ProviderType.KAKAO)
.orElseThrow(() -> new MemberException(MemberErrorCode.MEMBER_NOT_FOUND));

PrincipalDetails userDetails = new PrincipalDetails(member);
String jwtAccessToken = jwtProvider.createJwtAccessToken(userDetails);
String jwtRefreshToken = jwtProvider.createJwtRefreshToken(userDetails);

return MemberResponseDTO.LoginResultDTO.builder()
.userId(member.getId())
.createdAt(LocalDateTime.now())
.accessToken(jwtAccessToken)
.refreshToken(jwtRefreshToken)
.build();
});
}
}

0 comments on commit a02a06a

Please sign in to comment.