Skip to content

Commit

Permalink
feat: 카카오 로그 추가 - #128
Browse files Browse the repository at this point in the history
  • Loading branch information
sjk4618 committed Jan 22, 2025
1 parent b219ef9 commit 2b0d2a0
Show file tree
Hide file tree
Showing 8 changed files with 56 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

public class UserBadRequestException extends UserApiBaseException{

public UserBadRequestException(ErrorCode errorCode) {
public UserBadRequestException(final ErrorCode errorCode) {
super(errorCode);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,18 @@ public enum UserErrorCode implements ErrorCode {
/**
* 404 Not Found
*/
KAKAO_LOGIN_FAILED(HttpStatus.BAD_REQUEST, 40030, "카카오 로그인에 실패하였습니다"),


/**
* 404 Not Found
*/
USER_SOCIAL_TYPE_NOT_FOUND(HttpStatus.NOT_FOUND, 40430, "유저의 소셜타입을 찾을 수 없습니다."),
USER_NOT_FOUND(HttpStatus.NOT_FOUND, 40431, "유저를 찾을 수 없습니다."),

/**
* 500 Server Internal Error
*/
KAKAO_LOGIN_FAILED(HttpStatus.BAD_REQUEST, 50030, "카카오 로그인에 실패하였습니다"),
;

private final HttpStatus httpStatus;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package com.cakey.user.exception;

import com.cakey.rescode.ErrorCode;
import org.springframework.http.HttpStatus;

public class UserKakaoException extends UserApiBaseException{
public UserKakaoException(final ErrorCode errorCode) {
super(errorCode);
}

@Override
HttpStatus getStatus() {
return HttpStatus.INTERNAL_SERVER_ERROR;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

public class UserNotFoundException extends UserApiBaseException {

public UserNotFoundException(ErrorCode errorCode) {
public UserNotFoundException(final ErrorCode errorCode) {
super(errorCode);
}

Expand Down
14 changes: 10 additions & 4 deletions cakey-api/src/main/java/com/cakey/user/service/UserService.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,29 +6,31 @@
import com.cakey.client.kakao.api.KakaoSocialService;
import com.cakey.client.kakao.api.dto.KakaoUserDto;
import com.cakey.client.kakao.api.dto.UserCreateDto;
import com.cakey.exception.AuthKakaoException;
import com.cakey.jwt.auth.JwtProvider;
import com.cakey.jwt.domain.Token;
import com.cakey.jwt.domain.UserRole;
import com.cakey.user.domain.User;
import com.cakey.rescode.ErrorCode;
import com.cakey.user.dto.LoginSuccessRes;
import com.cakey.common.exception.NotFoundBaseException;
import com.cakey.user.dto.UserInfoDto;
import com.cakey.user.dto.UserInfoRes;
import com.cakey.user.exception.UserBadRequestException;
import com.cakey.user.exception.UserErrorCode;
import com.cakey.user.exception.UserKakaoException;
import com.cakey.user.exception.UserNotFoundException;
import com.cakey.user.facade.UserFacade;
import com.cakey.user.facade.UserRetriever;
import jakarta.servlet.http.HttpServletResponse;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.http.ResponseCookie;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import java.util.Optional;

@Service
@Slf4j
@RequiredArgsConstructor
public class UserService {

Expand All @@ -50,7 +52,11 @@ public LoginSuccessRes login(
final KakaoUserDto kakaoUserInfo;

if (loginReq.socialType().equals(SocialType.KAKAO)) {
kakaoUserInfo = kakaoSocialService.getKakaoUserInfo(authorizationCode, loginReq.redirectUri());
try {
kakaoUserInfo = kakaoSocialService.getKakaoUserInfo(authorizationCode, loginReq.redirectUri());
} catch (AuthKakaoException e) {
throw new UserKakaoException(UserErrorCode.KAKAO_LOGIN_FAILED);
}
} else {
throw new UserBadRequestException(UserErrorCode.KAKAO_LOGIN_FAILED);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,18 @@
import com.cakey.client.kakao.api.dto.KakaoAccessTokenRes;
import com.cakey.client.kakao.api.dto.KakaoUserDto;
import com.cakey.client.SocialType;
import com.cakey.exception.AuthKakaoException;
import com.cakey.jwt.domain.UserRole;
import com.cakey.rescode.ErrorBaseCode;
import feign.FeignException;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Service;

@Service
@Slf4j
@RequiredArgsConstructor
public class KakaoSocialService {
private final KakaoApiClient kakaoApiClient;
Expand All @@ -23,12 +27,13 @@ public class KakaoSocialService {
private String clientId;

public KakaoUserDto getKakaoUserInfo(final String authorizationCode, final String redirectUri) {
String kakaoAccessToken;
final String kakaoAccessToken;
try {
// 인가 코드로 카카오 Access Token 받아오기
kakaoAccessToken = getOAuth2Authentication(authorizationCode, redirectUri);
} catch (FeignException e) {
throw new RuntimeException("authentication code expired");
log.error(e.getMessage(), e);
throw new AuthKakaoException();
}

final String contentType = MediaType.APPLICATION_FORM_URLENCODED.toString();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.cakey.exception;

import com.cakey.rescode.ErrorCode;
import lombok.AccessLevel;
import lombok.Getter;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpStatus;

public abstract class AuthBaseException extends CakeBaseException { }
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package com.cakey.exception;

import com.cakey.rescode.ErrorCode;
import org.springframework.http.HttpStatus;


public class AuthKakaoException extends AuthBaseException {
}

0 comments on commit 2b0d2a0

Please sign in to comment.