From 2ce2668462a488d61664874be9bf05e224af1342 Mon Sep 17 00:00:00 2001 From: gahee99 Date: Fri, 23 Jun 2023 21:23:13 +0900 Subject: [PATCH 1/4] =?UTF-8?q?feat:=20check=20socialUser,=20kakapAPI=20?= =?UTF-8?q?=ED=98=B8=EC=B6=9C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- build.gradle | 2 + .../com/with/picme/common/SocialType.java | 5 +++ .../picme/common/message/ErrorMessage.java | 11 +++++- .../java/com/with/picme/config/KakaoAuth.java | 14 +++++++ .../with/picme/controller/AuthController.java | 12 ++++-- .../dto/auth/AuthSocialCheckRequestDto.java | 14 +++++++ .../dto/auth/AuthSocialCheckResponseDto.java | 20 ++++++++++ .../picme/dto/auth/kakao/KakaoAccount.java | 16 ++++++++ .../with/picme/dto/auth/kakao/KakaoUser.java | 20 ++++++++++ .../dto/auth/kakao/KakaoUserResponseDto.java | 18 +++++++++ .../com/with/picme/service/AuthService.java | 7 ++-- .../with/picme/service/AuthServiceImpl.java | 38 +++++++++++++++++-- 12 files changed, 163 insertions(+), 14 deletions(-) create mode 100644 src/main/java/com/with/picme/common/SocialType.java create mode 100644 src/main/java/com/with/picme/config/KakaoAuth.java create mode 100644 src/main/java/com/with/picme/dto/auth/AuthSocialCheckRequestDto.java create mode 100644 src/main/java/com/with/picme/dto/auth/AuthSocialCheckResponseDto.java create mode 100644 src/main/java/com/with/picme/dto/auth/kakao/KakaoAccount.java create mode 100644 src/main/java/com/with/picme/dto/auth/kakao/KakaoUser.java create mode 100644 src/main/java/com/with/picme/dto/auth/kakao/KakaoUserResponseDto.java diff --git a/build.gradle b/build.gradle index 20c5b2c..56b7bb0 100644 --- a/build.gradle +++ b/build.gradle @@ -39,6 +39,8 @@ dependencies { implementation 'io.jsonwebtoken:jjwt-jackson:0.11.2' implementation 'org.springframework.boot:spring-boot-starter-validation' + // feignClient + implementation group: 'org.springframework.cloud', name: 'spring-cloud-starter-openfeign', version: '3.1.1' } tasks.named('test') { diff --git a/src/main/java/com/with/picme/common/SocialType.java b/src/main/java/com/with/picme/common/SocialType.java new file mode 100644 index 0000000..e19550c --- /dev/null +++ b/src/main/java/com/with/picme/common/SocialType.java @@ -0,0 +1,5 @@ +package com.with.picme.common; + +public enum SocialType { + kakao +} diff --git a/src/main/java/com/with/picme/common/message/ErrorMessage.java b/src/main/java/com/with/picme/common/message/ErrorMessage.java index cc9b214..cf2272e 100644 --- a/src/main/java/com/with/picme/common/message/ErrorMessage.java +++ b/src/main/java/com/with/picme/common/message/ErrorMessage.java @@ -23,7 +23,7 @@ public enum ErrorMessage { EMPTY_TOKEN("빈 토큰입니다."), INVALID_PASSWORD("잘못된 비밀번호입니다."), INVALID_EMAIL("잘못된 이메일입니다."), - + /** * exception **/ @@ -38,7 +38,14 @@ public enum ErrorMessage { /** * user */ - CANT_GET_USERINFO("유저 아이디를 갖고올 수 없습니다."); + CANT_GET_USERINFO("유저 아이디를 갖고올 수 없습니다."), + + + /* + * social + */ + NO_SOCIAL_TYPE("제공하는 소셜 서비스가 다릅니다."), + NO_SOCIAL_USER("소셜 서비스에 가입하지 않은 유저입니다"); private final String message; } diff --git a/src/main/java/com/with/picme/config/KakaoAuth.java b/src/main/java/com/with/picme/config/KakaoAuth.java new file mode 100644 index 0000000..57a9260 --- /dev/null +++ b/src/main/java/com/with/picme/config/KakaoAuth.java @@ -0,0 +1,14 @@ +package com.with.picme.config; + + +import com.with.picme.dto.auth.kakao.KakaoUserResponseDto; +import org.springframework.cloud.openfeign.FeignClient; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.RequestHeader; + +@FeignClient(name = "kakaoAuth", url = "https://kapi.kakao.com") +public interface KakaoAuth { + + @GetMapping("/v2 /user/me") + KakaoUserResponseDto getProfileInfo(@RequestHeader("Authorization") String accessToken); +} diff --git a/src/main/java/com/with/picme/controller/AuthController.java b/src/main/java/com/with/picme/controller/AuthController.java index 34cff9b..3ec57b7 100644 --- a/src/main/java/com/with/picme/controller/AuthController.java +++ b/src/main/java/com/with/picme/controller/AuthController.java @@ -1,10 +1,8 @@ package com.with.picme.controller; import com.with.picme.common.ApiResponse; -import com.with.picme.dto.auth.AuthSignInRequestDto; -import com.with.picme.dto.auth.AuthSignInResponseDto; -import com.with.picme.dto.auth.AuthSignUpRequestDto; -import com.with.picme.dto.auth.AuthSignUpResponseDto; +import com.with.picme.dto.auth.*; +import com.with.picme.dto.auth.kakao.KakaoUser; import com.with.picme.service.AuthServiceImpl; import lombok.RequiredArgsConstructor; import org.springframework.http.ResponseEntity; @@ -33,4 +31,10 @@ public ResponseEntity signInUser(@RequestBody AuthSignInRequestDto AuthSignInResponseDto response = authService.signInUser(request); return ResponseEntity.ok(ApiResponse.success(SUCCESS_SIGN_IN.getMessage(), response)); } + + @PostMapping("/kakao/check") + public ResponseEntity findSocialUser(@RequestBody @Valid AuthSocialCheckRequestDto request) { + KakaoUser user = authService.getUser(request); + return null; + } } diff --git a/src/main/java/com/with/picme/dto/auth/AuthSocialCheckRequestDto.java b/src/main/java/com/with/picme/dto/auth/AuthSocialCheckRequestDto.java new file mode 100644 index 0000000..3e70ed6 --- /dev/null +++ b/src/main/java/com/with/picme/dto/auth/AuthSocialCheckRequestDto.java @@ -0,0 +1,14 @@ +package com.with.picme.dto.auth; + + +import javax.validation.constraints.NotBlank; + + +public record AuthSocialCheckRequestDto( + @NotBlank(message="필요한 값이 없습니다") + String socialType, + + @NotBlank(message = "필요한 값이 없습니다.") + String token +) { +} diff --git a/src/main/java/com/with/picme/dto/auth/AuthSocialCheckResponseDto.java b/src/main/java/com/with/picme/dto/auth/AuthSocialCheckResponseDto.java new file mode 100644 index 0000000..1471fb0 --- /dev/null +++ b/src/main/java/com/with/picme/dto/auth/AuthSocialCheckResponseDto.java @@ -0,0 +1,20 @@ +package com.with.picme.dto.auth; + +import lombok.Builder; + +@Builder +public record AuthSocialCheckResponseDto ( + Long uid, + String email, + boolean isUser +){ + public static AuthSocialCheckResponseDto of(Long uid, String email, boolean isUser){ + return AuthSocialCheckResponseDto + .builder() + .uid(uid) + .email(email) + .isUser(isUser) + .build(); + + } +} diff --git a/src/main/java/com/with/picme/dto/auth/kakao/KakaoAccount.java b/src/main/java/com/with/picme/dto/auth/kakao/KakaoAccount.java new file mode 100644 index 0000000..82f243a --- /dev/null +++ b/src/main/java/com/with/picme/dto/auth/kakao/KakaoAccount.java @@ -0,0 +1,16 @@ +package com.with.picme.dto.auth.kakao; + +import com.with.picme.config.KakaoAuth; +import lombok.Builder; + +@Builder +public record KakaoAccount( + String email +) { + public static KakaoAccount of(String email){ + return KakaoAccount + .builder() + .email(email) + .build(); + } +} diff --git a/src/main/java/com/with/picme/dto/auth/kakao/KakaoUser.java b/src/main/java/com/with/picme/dto/auth/kakao/KakaoUser.java new file mode 100644 index 0000000..ba579b3 --- /dev/null +++ b/src/main/java/com/with/picme/dto/auth/kakao/KakaoUser.java @@ -0,0 +1,20 @@ +package com.with.picme.dto.auth.kakao; + +import com.with.picme.config.KakaoAuth; +import lombok.Builder; + +@Builder +public record KakaoUser( + Long userId, + String email, + String providerType +) { + public static KakaoUser of(Long userId, String email){ + return KakaoUser + .builder() + .userId(userId) + .email(email) + .providerType("kakao") + .build(); + } +} diff --git a/src/main/java/com/with/picme/dto/auth/kakao/KakaoUserResponseDto.java b/src/main/java/com/with/picme/dto/auth/kakao/KakaoUserResponseDto.java new file mode 100644 index 0000000..26e0746 --- /dev/null +++ b/src/main/java/com/with/picme/dto/auth/kakao/KakaoUserResponseDto.java @@ -0,0 +1,18 @@ +package com.with.picme.dto.auth.kakao; + + +import lombok.Builder; + +@Builder +public record KakaoUserResponseDto( + Long id, + KakaoAccount kakao_account +) { + public static KakaoUserResponseDto of(Long id, KakaoAccount kakaoAccount) { + return KakaoUserResponseDto + .builder() + .id(id) + .kakao_account(kakaoAccount) + .build(); + } +} diff --git a/src/main/java/com/with/picme/service/AuthService.java b/src/main/java/com/with/picme/service/AuthService.java index 1c5ed90..c7dacd6 100644 --- a/src/main/java/com/with/picme/service/AuthService.java +++ b/src/main/java/com/with/picme/service/AuthService.java @@ -1,12 +1,11 @@ package com.with.picme.service; -import com.with.picme.dto.auth.AuthSignInRequestDto; -import com.with.picme.dto.auth.AuthSignInResponseDto; -import com.with.picme.dto.auth.AuthSignUpRequestDto; -import com.with.picme.dto.auth.AuthSignUpResponseDto; +import com.with.picme.dto.auth.*; +import com.with.picme.dto.auth.kakao.KakaoUser; public interface AuthService { AuthSignUpResponseDto createUser(AuthSignUpRequestDto request); AuthSignInResponseDto signInUser(AuthSignInRequestDto request); + KakaoUser getUser(AuthSocialCheckRequestDto request); } diff --git a/src/main/java/com/with/picme/service/AuthServiceImpl.java b/src/main/java/com/with/picme/service/AuthServiceImpl.java index 11fd807..caf78f6 100644 --- a/src/main/java/com/with/picme/service/AuthServiceImpl.java +++ b/src/main/java/com/with/picme/service/AuthServiceImpl.java @@ -1,12 +1,14 @@ package com.with.picme.service; +import com.with.picme.common.SocialType; +import com.with.picme.config.KakaoAuth; import com.with.picme.config.SaltEncrypt; import com.with.picme.config.jwt.JwtTokenProvider; import com.with.picme.config.jwt.UserAuthentication; -import com.with.picme.dto.auth.AuthSignInRequestDto; -import com.with.picme.dto.auth.AuthSignInResponseDto; -import com.with.picme.dto.auth.AuthSignUpRequestDto; -import com.with.picme.dto.auth.AuthSignUpResponseDto; +import com.with.picme.dto.auth.*; +import com.with.picme.dto.auth.kakao.KakaoAccount; +import com.with.picme.dto.auth.kakao.KakaoUser; +import com.with.picme.dto.auth.kakao.KakaoUserResponseDto; import com.with.picme.entity.User; import com.with.picme.repository.UserRepository; import lombok.RequiredArgsConstructor; @@ -25,6 +27,7 @@ public class AuthServiceImpl implements AuthService { private final UserRepository userRepository; private final SaltEncrypt saltEncrypt; private final JwtTokenProvider tokenProvider; + private final KakaoAuth kakaoAuth; @Override public AuthSignUpResponseDto createUser(AuthSignUpRequestDto request) { @@ -66,6 +69,19 @@ public AuthSignInResponseDto signInUser(AuthSignInRequestDto request) { return AuthSignInResponseDto.of(user, accessToken); } + @Override + public KakaoUser getUser(AuthSocialCheckRequestDto request) { + if (!request.socialType().equals(SocialType.kakao)){ + throw new IllegalArgumentException(NO_SOCIAL_TYPE.getMessage()); + } + //카카오 계정 확인 + KakaoUserResponseDto user = kakaoAuth.getProfileInfo(request.token()); + checkSocialUser(user); + // 이메일 확인 -> 이메일 없는 경우 ""으로 대체 + KakaoUserResponseDto kakaoUser = checkSocialUserHaveEmail(user); + return KakaoUser.of(kakaoUser.id(), kakaoUser.kakao_account().email()); + } + private User checkPassword(String email, String password) { User user = userRepository.findByEmail(email); if (saltEncrypt.isMatch(password, user.getPassword())) @@ -73,4 +89,18 @@ private User checkPassword(String email, String password) { else throw new IllegalArgumentException(INVALID_PASSWORD.getMessage()); } + + private boolean checkSocialUser(KakaoUserResponseDto kakaoUser){ + if (kakaoUser.id() != null){ + throw new IllegalArgumentException(NO_SOCIAL_USER.getMessage()); + } + return true; + } + + private KakaoUserResponseDto checkSocialUserHaveEmail(KakaoUserResponseDto kakaoUserResponseDto){ + if (kakaoUserResponseDto.kakao_account().email() == null){ + return KakaoUserResponseDto.of(kakaoUserResponseDto.id(), KakaoAccount.of("")); + } + return kakaoUserResponseDto; + } } From 39d3020108b70e939ffaf94f54f83033e269773c Mon Sep 17 00:00:00 2001 From: gahee99 Date: Mon, 26 Jun 2023 01:15:30 +0900 Subject: [PATCH 2/4] =?UTF-8?q?feat:=20kakao=20=EC=9C=A0=EC=A0=80=20check?= =?UTF-8?q?=20=EA=B5=AC=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/com/with/picme/PicmeApplication.java | 2 + .../picme/common/message/ErrorMessage.java | 3 +- .../picme/common/message/ResponseMessage.java | 6 ++- .../picme/config/{ => kakao}/KakaoAuth.java | 4 +- .../picme/config/kakao/KakaoAuthImpl.java | 49 +++++++++++++++++ .../with/picme/controller/AuthController.java | 21 ++++++-- .../picme/dto/auth/kakao/KakaoAccount.java | 1 - .../with/picme/dto/auth/kakao/KakaoUser.java | 7 +-- .../picme/entity/AuthenticationProvider.java | 2 + .../com/with/picme/entity/ProviderType.java | 4 ++ .../AuthenticationProviderRepository.java | 17 ++++++ .../com/with/picme/service/AuthService.java | 6 +++ .../with/picme/service/AuthServiceImpl.java | 52 +++++++++---------- 13 files changed, 135 insertions(+), 39 deletions(-) rename src/main/java/com/with/picme/config/{ => kakao}/KakaoAuth.java (86%) create mode 100644 src/main/java/com/with/picme/config/kakao/KakaoAuthImpl.java create mode 100644 src/main/java/com/with/picme/repository/AuthenticationProviderRepository.java diff --git a/src/main/java/com/with/picme/PicmeApplication.java b/src/main/java/com/with/picme/PicmeApplication.java index 3876709..2692854 100644 --- a/src/main/java/com/with/picme/PicmeApplication.java +++ b/src/main/java/com/with/picme/PicmeApplication.java @@ -2,7 +2,9 @@ import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.cloud.openfeign.EnableFeignClients; +@EnableFeignClients @SpringBootApplication public class PicmeApplication { diff --git a/src/main/java/com/with/picme/common/message/ErrorMessage.java b/src/main/java/com/with/picme/common/message/ErrorMessage.java index cf2272e..6dd4866 100644 --- a/src/main/java/com/with/picme/common/message/ErrorMessage.java +++ b/src/main/java/com/with/picme/common/message/ErrorMessage.java @@ -45,7 +45,8 @@ public enum ErrorMessage { * social */ NO_SOCIAL_TYPE("제공하는 소셜 서비스가 다릅니다."), - NO_SOCIAL_USER("소셜 서비스에 가입하지 않은 유저입니다"); + NO_SOCIAL_USER("소셜 서비스에 가입하지 않은 유저입니다"), + NOT_FOUND_SOCIAL_TOKEN("소셜로그인 토큰이 유효하지 않습니다."); private final String message; } diff --git a/src/main/java/com/with/picme/common/message/ResponseMessage.java b/src/main/java/com/with/picme/common/message/ResponseMessage.java index 4754dd4..c7c9391 100644 --- a/src/main/java/com/with/picme/common/message/ResponseMessage.java +++ b/src/main/java/com/with/picme/common/message/ResponseMessage.java @@ -21,8 +21,12 @@ public enum ResponseMessage { /* user */ - GET_USER_INFO("유저 정보 갖고오기 성공"); + GET_USER_INFO("유저 정보 갖고오기 성공"), + /* + social + */ + CHECK_KAKAO_USER_SUCCESS("카카오 계정 확인 성공"); private final String message; } diff --git a/src/main/java/com/with/picme/config/KakaoAuth.java b/src/main/java/com/with/picme/config/kakao/KakaoAuth.java similarity index 86% rename from src/main/java/com/with/picme/config/KakaoAuth.java rename to src/main/java/com/with/picme/config/kakao/KakaoAuth.java index 57a9260..5c74d6c 100644 --- a/src/main/java/com/with/picme/config/KakaoAuth.java +++ b/src/main/java/com/with/picme/config/kakao/KakaoAuth.java @@ -1,4 +1,4 @@ -package com.with.picme.config; +package com.with.picme.config.kakao; import com.with.picme.dto.auth.kakao.KakaoUserResponseDto; @@ -9,6 +9,6 @@ @FeignClient(name = "kakaoAuth", url = "https://kapi.kakao.com") public interface KakaoAuth { - @GetMapping("/v2 /user/me") + @GetMapping("/v2/user/me") KakaoUserResponseDto getProfileInfo(@RequestHeader("Authorization") String accessToken); } diff --git a/src/main/java/com/with/picme/config/kakao/KakaoAuthImpl.java b/src/main/java/com/with/picme/config/kakao/KakaoAuthImpl.java new file mode 100644 index 0000000..84d21cd --- /dev/null +++ b/src/main/java/com/with/picme/config/kakao/KakaoAuthImpl.java @@ -0,0 +1,49 @@ +package com.with.picme.config.kakao; + +import com.with.picme.common.message.ErrorMessage; +import com.with.picme.dto.auth.kakao.KakaoUser; +import com.with.picme.dto.auth.kakao.KakaoUserResponseDto; +import lombok.RequiredArgsConstructor; +import org.springframework.stereotype.Component; + +import javax.persistence.EntityNotFoundException; + +import static com.with.picme.common.message.ErrorMessage.*; + +@RequiredArgsConstructor +@Component +public class KakaoAuthImpl implements KakaoAuth { + private final KakaoAuth kakaoAuth; + + @Override + public KakaoUserResponseDto getProfileInfo(String accessToken) { + try { + KakaoUserResponseDto kakaoProfile = kakaoAuth.getProfileInfo(accessToken); + return kakaoProfile; + } catch (Exception e) { + throw new EntityNotFoundException(ErrorMessage.NOT_FOUND_SOCIAL_TOKEN.getMessage()); + } + } + + public KakaoUser getKakaoUser(String accessToken){ + KakaoUserResponseDto user = this.getProfileInfo(accessToken); + checkSocialUser(user); + KakaoUser kakaoUser = checkSocialUserHaveEmail(user); + return kakaoUser; + } + + private boolean checkSocialUser(KakaoUserResponseDto kakaoUser){ + if (kakaoUser.id() == null){ + throw new IllegalArgumentException(NO_SOCIAL_USER.getMessage()); + } + return true; + } + + private KakaoUser checkSocialUserHaveEmail(KakaoUserResponseDto kakaoUserResponseDto){ + String email = ""; + if (kakaoUserResponseDto.kakao_account().email() != null) { + email = kakaoUserResponseDto.kakao_account().email(); + } + return KakaoUser.of(kakaoUserResponseDto.id(), email); + } +} diff --git a/src/main/java/com/with/picme/controller/AuthController.java b/src/main/java/com/with/picme/controller/AuthController.java index 3ec57b7..250cd71 100644 --- a/src/main/java/com/with/picme/controller/AuthController.java +++ b/src/main/java/com/with/picme/controller/AuthController.java @@ -3,6 +3,8 @@ import com.with.picme.common.ApiResponse; import com.with.picme.dto.auth.*; import com.with.picme.dto.auth.kakao.KakaoUser; +import com.with.picme.entity.User; +import com.with.picme.repository.AuthenticationProviderRepository; import com.with.picme.service.AuthServiceImpl; import lombok.RequiredArgsConstructor; import org.springframework.http.ResponseEntity; @@ -11,14 +13,16 @@ import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import javax.validation.Valid; -import static com.with.picme.common.message.ResponseMessage.SUCCESS_SIGN_IN; -import static com.with.picme.common.message.ResponseMessage.SUCCESS_SIGN_UP; + + +import static com.with.picme.common.message.ResponseMessage.*; @RestController @RequestMapping("/auth") @RequiredArgsConstructor public class AuthController { private final AuthServiceImpl authService; + private final AuthenticationProviderRepository authenticationProviderRepository; @PostMapping("") public ResponseEntity createUser(@RequestBody @Valid AuthSignUpRequestDto request) { @@ -33,8 +37,15 @@ public ResponseEntity signInUser(@RequestBody AuthSignInRequestDto } @PostMapping("/kakao/check") - public ResponseEntity findSocialUser(@RequestBody @Valid AuthSocialCheckRequestDto request) { - KakaoUser user = authService.getUser(request); - return null; + public ResponseEntity findSocialUser(@RequestBody AuthSocialCheckRequestDto request) { + KakaoUser user = authService.getUser(request); + User existUser = authService.findByKey(user); + AuthSocialCheckResponseDto response ; + if (existUser==null) + response = AuthSocialCheckResponseDto.of(user.userId(),user.email(),false); + else{ + response = AuthSocialCheckResponseDto.of(user.userId(),user.email(),true); + } + return ResponseEntity.ok(ApiResponse.success(CHECK_KAKAO_USER_SUCCESS.getMessage(),response)); } } diff --git a/src/main/java/com/with/picme/dto/auth/kakao/KakaoAccount.java b/src/main/java/com/with/picme/dto/auth/kakao/KakaoAccount.java index 82f243a..b320aae 100644 --- a/src/main/java/com/with/picme/dto/auth/kakao/KakaoAccount.java +++ b/src/main/java/com/with/picme/dto/auth/kakao/KakaoAccount.java @@ -1,6 +1,5 @@ package com.with.picme.dto.auth.kakao; -import com.with.picme.config.KakaoAuth; import lombok.Builder; @Builder diff --git a/src/main/java/com/with/picme/dto/auth/kakao/KakaoUser.java b/src/main/java/com/with/picme/dto/auth/kakao/KakaoUser.java index ba579b3..00eeaf2 100644 --- a/src/main/java/com/with/picme/dto/auth/kakao/KakaoUser.java +++ b/src/main/java/com/with/picme/dto/auth/kakao/KakaoUser.java @@ -1,20 +1,21 @@ package com.with.picme.dto.auth.kakao; -import com.with.picme.config.KakaoAuth; + +import com.with.picme.entity.ProviderType; import lombok.Builder; @Builder public record KakaoUser( Long userId, String email, - String providerType + ProviderType providerType ) { public static KakaoUser of(Long userId, String email){ return KakaoUser .builder() .userId(userId) .email(email) - .providerType("kakao") + .providerType(ProviderType.kakao) .build(); } } diff --git a/src/main/java/com/with/picme/entity/AuthenticationProvider.java b/src/main/java/com/with/picme/entity/AuthenticationProvider.java index 3be4b08..b059108 100644 --- a/src/main/java/com/with/picme/entity/AuthenticationProvider.java +++ b/src/main/java/com/with/picme/entity/AuthenticationProvider.java @@ -13,6 +13,7 @@ @Setter @Entity @NoArgsConstructor +@Table(name = "\"AuthenticationProvider\"") public class AuthenticationProvider { @Id @GeneratedValue(strategy = IDENTITY) @@ -20,6 +21,7 @@ public class AuthenticationProvider { private Long id; @Column(name="provider_type") + @Enumerated(EnumType.STRING) private ProviderType provider; @OneToOne(fetch = FetchType.LAZY) diff --git a/src/main/java/com/with/picme/entity/ProviderType.java b/src/main/java/com/with/picme/entity/ProviderType.java index e099149..ebb53f3 100644 --- a/src/main/java/com/with/picme/entity/ProviderType.java +++ b/src/main/java/com/with/picme/entity/ProviderType.java @@ -1,5 +1,9 @@ package com.with.picme.entity; +import javax.persistence.EnumType; +import javax.persistence.Enumerated; + public enum ProviderType { + @Enumerated(EnumType.STRING) kakao, naver, google } diff --git a/src/main/java/com/with/picme/repository/AuthenticationProviderRepository.java b/src/main/java/com/with/picme/repository/AuthenticationProviderRepository.java new file mode 100644 index 0000000..eae2c6a --- /dev/null +++ b/src/main/java/com/with/picme/repository/AuthenticationProviderRepository.java @@ -0,0 +1,17 @@ +package com.with.picme.repository; + +import com.with.picme.entity.AuthenticationProvider; +import com.with.picme.entity.ProviderType; +import org.springframework.data.jpa.repository.EntityGraph; +import org.springframework.data.jpa.repository.JpaRepository; + + +import java.util.Optional; + +public interface AuthenticationProviderRepository extends JpaRepository { + @EntityGraph(attributePaths = "user") + Optional findById(Long id); + + @EntityGraph(attributePaths = "user") + Optional findByIdAndProvider(Long id, ProviderType providerType); +} diff --git a/src/main/java/com/with/picme/service/AuthService.java b/src/main/java/com/with/picme/service/AuthService.java index c7dacd6..677d56c 100644 --- a/src/main/java/com/with/picme/service/AuthService.java +++ b/src/main/java/com/with/picme/service/AuthService.java @@ -2,10 +2,16 @@ import com.with.picme.dto.auth.*; import com.with.picme.dto.auth.kakao.KakaoUser; +import com.with.picme.entity.AuthenticationProvider; +import com.with.picme.entity.User; + +import java.util.Optional; public interface AuthService { AuthSignUpResponseDto createUser(AuthSignUpRequestDto request); AuthSignInResponseDto signInUser(AuthSignInRequestDto request); KakaoUser getUser(AuthSocialCheckRequestDto request); + + User findByKey(KakaoUser of); } diff --git a/src/main/java/com/with/picme/service/AuthServiceImpl.java b/src/main/java/com/with/picme/service/AuthServiceImpl.java index caf78f6..66c98d8 100644 --- a/src/main/java/com/with/picme/service/AuthServiceImpl.java +++ b/src/main/java/com/with/picme/service/AuthServiceImpl.java @@ -1,15 +1,16 @@ package com.with.picme.service; import com.with.picme.common.SocialType; -import com.with.picme.config.KakaoAuth; +import com.with.picme.common.message.ErrorMessage; +import com.with.picme.config.kakao.KakaoAuthImpl; import com.with.picme.config.SaltEncrypt; import com.with.picme.config.jwt.JwtTokenProvider; import com.with.picme.config.jwt.UserAuthentication; import com.with.picme.dto.auth.*; -import com.with.picme.dto.auth.kakao.KakaoAccount; import com.with.picme.dto.auth.kakao.KakaoUser; -import com.with.picme.dto.auth.kakao.KakaoUserResponseDto; +import com.with.picme.entity.AuthenticationProvider; import com.with.picme.entity.User; +import com.with.picme.repository.AuthenticationProviderRepository; import com.with.picme.repository.UserRepository; import lombok.RequiredArgsConstructor; import org.springframework.security.core.Authentication; @@ -18,6 +19,8 @@ import javax.persistence.EntityNotFoundException; +import java.util.Optional; + import static com.with.picme.common.message.ErrorMessage.*; @RequiredArgsConstructor @@ -27,8 +30,8 @@ public class AuthServiceImpl implements AuthService { private final UserRepository userRepository; private final SaltEncrypt saltEncrypt; private final JwtTokenProvider tokenProvider; - private final KakaoAuth kakaoAuth; - + private final KakaoAuthImpl kakaoAuthImpl; + private final AuthenticationProviderRepository authenticationProviderRepository; @Override public AuthSignUpResponseDto createUser(AuthSignUpRequestDto request) { if (validateEmail(request.email())) @@ -71,15 +74,26 @@ public AuthSignInResponseDto signInUser(AuthSignInRequestDto request) { @Override public KakaoUser getUser(AuthSocialCheckRequestDto request) { - if (!request.socialType().equals(SocialType.kakao)){ + if (!request.socialType().equals(SocialType.kakao.toString())) { //토큰 타입 확인 throw new IllegalArgumentException(NO_SOCIAL_TYPE.getMessage()); } - //카카오 계정 확인 - KakaoUserResponseDto user = kakaoAuth.getProfileInfo(request.token()); - checkSocialUser(user); - // 이메일 확인 -> 이메일 없는 경우 ""으로 대체 - KakaoUserResponseDto kakaoUser = checkSocialUserHaveEmail(user); - return KakaoUser.of(kakaoUser.id(), kakaoUser.kakao_account().email()); + return kakaoAuthImpl.getKakaoUser("Bearer " + request.token()); //카카오 계정 확인 + } + + public User findByKey(KakaoUser kakaoUser){ + Optional authenticationProvider = authenticationProviderRepository + .findByIdAndProvider(kakaoUser.userId(), + kakaoUser.providerType()); + // 카카오 계정은 확인, 우리 서비스에 이미 로그인함 + if(authenticationProvider.isPresent()) { + Long userId = authenticationProvider.get().getUser().getId(); + //유저 테이블에서 찾기 , 못찾을 경우 에러 날려야 하는지 궁굼.. ->node에서는 return null로 날림 + User user = userRepository.findById(userId) + .orElseThrow(() -> new EntityNotFoundException(ErrorMessage.CANT_GET_USERINFO.getMessage())); + return user; + } + // 카카오 계정은 확인 됐지만, 우리 서비스에는 로그인 안됨 + return null; } private User checkPassword(String email, String password) { @@ -89,18 +103,4 @@ private User checkPassword(String email, String password) { else throw new IllegalArgumentException(INVALID_PASSWORD.getMessage()); } - - private boolean checkSocialUser(KakaoUserResponseDto kakaoUser){ - if (kakaoUser.id() != null){ - throw new IllegalArgumentException(NO_SOCIAL_USER.getMessage()); - } - return true; - } - - private KakaoUserResponseDto checkSocialUserHaveEmail(KakaoUserResponseDto kakaoUserResponseDto){ - if (kakaoUserResponseDto.kakao_account().email() == null){ - return KakaoUserResponseDto.of(kakaoUserResponseDto.id(), KakaoAccount.of("")); - } - return kakaoUserResponseDto; - } } From b3434d2373511706b0bead45879c336b7f56fca3 Mon Sep 17 00:00:00 2001 From: gahee99 Date: Mon, 26 Jun 2023 01:26:32 +0900 Subject: [PATCH 3/4] =?UTF-8?q?fix:=20=EB=B6=88=ED=95=84=EC=9A=94=ED=95=9C?= =?UTF-8?q?=20=EC=BD=94=EB=93=9C=20=EC=82=AD=EC=A0=9C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../picme/repository/AuthenticationProviderRepository.java | 3 --- 1 file changed, 3 deletions(-) diff --git a/src/main/java/com/with/picme/repository/AuthenticationProviderRepository.java b/src/main/java/com/with/picme/repository/AuthenticationProviderRepository.java index eae2c6a..89c2a84 100644 --- a/src/main/java/com/with/picme/repository/AuthenticationProviderRepository.java +++ b/src/main/java/com/with/picme/repository/AuthenticationProviderRepository.java @@ -9,9 +9,6 @@ import java.util.Optional; public interface AuthenticationProviderRepository extends JpaRepository { - @EntityGraph(attributePaths = "user") - Optional findById(Long id); - @EntityGraph(attributePaths = "user") Optional findByIdAndProvider(Long id, ProviderType providerType); } From 6bcb9b64a7ed8399624738a234fd810cf1a67fee Mon Sep 17 00:00:00 2001 From: gahee99 Date: Wed, 28 Jun 2023 20:47:00 +0900 Subject: [PATCH 4/4] =?UTF-8?q?fix/#14:=20code=20review=EB=B0=98=EC=98=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../picme/config/kakao/KakaoAuthImpl.java | 6 +++-- .../with/picme/controller/AuthController.java | 9 +------ .../com/with/picme/service/AuthService.java | 4 +--- .../with/picme/service/AuthServiceImpl.java | 24 +++++++++---------- 4 files changed, 17 insertions(+), 26 deletions(-) diff --git a/src/main/java/com/with/picme/config/kakao/KakaoAuthImpl.java b/src/main/java/com/with/picme/config/kakao/KakaoAuthImpl.java index 84d21cd..f0977a8 100644 --- a/src/main/java/com/with/picme/config/kakao/KakaoAuthImpl.java +++ b/src/main/java/com/with/picme/config/kakao/KakaoAuthImpl.java @@ -27,14 +27,16 @@ public KakaoUserResponseDto getProfileInfo(String accessToken) { public KakaoUser getKakaoUser(String accessToken){ KakaoUserResponseDto user = this.getProfileInfo(accessToken); - checkSocialUser(user); + if(!checkSocialUser(user)){ + throw new IllegalArgumentException(NO_SOCIAL_USER.getMessage()); + }; KakaoUser kakaoUser = checkSocialUserHaveEmail(user); return kakaoUser; } private boolean checkSocialUser(KakaoUserResponseDto kakaoUser){ if (kakaoUser.id() == null){ - throw new IllegalArgumentException(NO_SOCIAL_USER.getMessage()); + return false; } return true; } diff --git a/src/main/java/com/with/picme/controller/AuthController.java b/src/main/java/com/with/picme/controller/AuthController.java index 250cd71..e210f99 100644 --- a/src/main/java/com/with/picme/controller/AuthController.java +++ b/src/main/java/com/with/picme/controller/AuthController.java @@ -38,14 +38,7 @@ public ResponseEntity signInUser(@RequestBody AuthSignInRequestDto @PostMapping("/kakao/check") public ResponseEntity findSocialUser(@RequestBody AuthSocialCheckRequestDto request) { - KakaoUser user = authService.getUser(request); - User existUser = authService.findByKey(user); - AuthSocialCheckResponseDto response ; - if (existUser==null) - response = AuthSocialCheckResponseDto.of(user.userId(),user.email(),false); - else{ - response = AuthSocialCheckResponseDto.of(user.userId(),user.email(),true); - } + AuthSocialCheckResponseDto response = authService.findSocialUser(request); return ResponseEntity.ok(ApiResponse.success(CHECK_KAKAO_USER_SUCCESS.getMessage(),response)); } } diff --git a/src/main/java/com/with/picme/service/AuthService.java b/src/main/java/com/with/picme/service/AuthService.java index 677d56c..a49edd7 100644 --- a/src/main/java/com/with/picme/service/AuthService.java +++ b/src/main/java/com/with/picme/service/AuthService.java @@ -11,7 +11,5 @@ public interface AuthService { AuthSignUpResponseDto createUser(AuthSignUpRequestDto request); AuthSignInResponseDto signInUser(AuthSignInRequestDto request); - KakaoUser getUser(AuthSocialCheckRequestDto request); - - User findByKey(KakaoUser of); + AuthSocialCheckResponseDto findSocialUser(AuthSocialCheckRequestDto request); } diff --git a/src/main/java/com/with/picme/service/AuthServiceImpl.java b/src/main/java/com/with/picme/service/AuthServiceImpl.java index 66c98d8..647cbd6 100644 --- a/src/main/java/com/with/picme/service/AuthServiceImpl.java +++ b/src/main/java/com/with/picme/service/AuthServiceImpl.java @@ -72,28 +72,26 @@ public AuthSignInResponseDto signInUser(AuthSignInRequestDto request) { return AuthSignInResponseDto.of(user, accessToken); } + @Override - public KakaoUser getUser(AuthSocialCheckRequestDto request) { + public AuthSocialCheckResponseDto findSocialUser(AuthSocialCheckRequestDto request) { if (!request.socialType().equals(SocialType.kakao.toString())) { //토큰 타입 확인 throw new IllegalArgumentException(NO_SOCIAL_TYPE.getMessage()); } - return kakaoAuthImpl.getKakaoUser("Bearer " + request.token()); //카카오 계정 확인 - } + KakaoUser kakaoUser = kakaoAuthImpl.getKakaoUser("Bearer " + request.token()); //카카오 계정 확인 - public User findByKey(KakaoUser kakaoUser){ - Optional authenticationProvider = authenticationProviderRepository - .findByIdAndProvider(kakaoUser.userId(), - kakaoUser.providerType()); - // 카카오 계정은 확인, 우리 서비스에 이미 로그인함 + Optional authenticationProvider = authenticationProviderRepository.findByIdAndProvider(kakaoUser.userId(), kakaoUser.providerType()); + + // 카카오 계정으로 우리 서비스에 회원가입 함 if(authenticationProvider.isPresent()) { Long userId = authenticationProvider.get().getUser().getId(); - //유저 테이블에서 찾기 , 못찾을 경우 에러 날려야 하는지 궁굼.. ->node에서는 return null로 날림 - User user = userRepository.findById(userId) + //유저 테이블에서 찾기 + userRepository.findById(userId) .orElseThrow(() -> new EntityNotFoundException(ErrorMessage.CANT_GET_USERINFO.getMessage())); - return user; + return AuthSocialCheckResponseDto.of(kakaoUser.userId(),kakaoUser.email(),true); } - // 카카오 계정은 확인 됐지만, 우리 서비스에는 로그인 안됨 - return null; + // 카카오 계정은 확인, 우리 서비스에는 회원가입 안됨 + return AuthSocialCheckResponseDto.of(kakaoUser.userId(),kakaoUser.email(),false); } private User checkPassword(String email, String password) {