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

[Refactor] 엔티티 Setter 제거 #59

Merged
merged 5 commits into from
Apr 4, 2024
Merged
Changes from 1 commit
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
Prev Previous commit
refactor: Modify AuthService (#56)
Co-authored-by: Ahyun0326 <[email protected]>
  • Loading branch information
chaeeun-Han and Ahyun0326 committed Apr 4, 2024
commit ac3931f550bca5b69cf07bb4cc2283cef19b84f9
13 changes: 6 additions & 7 deletions src/main/java/com/beotkkot/qtudy/service/auth/AuthService.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import com.beotkkot.qtudy.dto.object.KakaoUserInfo;
import com.beotkkot.qtudy.dto.response.ResponseDto;
import com.beotkkot.qtudy.dto.response.auth.AuthResponseDto;
import com.beotkkot.qtudy.repository.user.UserRepository;
import com.beotkkot.qtudy.service.user.UserService;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
Expand Down Expand Up @@ -31,6 +32,7 @@ public class AuthService {
@Value("${OAUTH_CLIENT_ID}")
private String OAUTH_CLIENT_ID;
private final UserService userService;
private final UserRepository userRepository;

// 1. 코드를 이용해 카카오로부터 토큰 얻기
public String getAccessToken(String code) {
Expand All @@ -50,7 +52,7 @@ public String getAccessToken(String code) {
MultiValueMap<String, String> params = new LinkedMultiValueMap<>();
params.add("grant_type","authorization_code");
params.add("client_id", OAUTH_CLIENT_ID);
params.add("redirect_uti", "https:https://qtudy.site/auth/redirected/kakao");
params.add("redirect_uri", "http:https://localhost:3000/auth/redirected/kakao");
params.add("code", code);

// header, body를 가진 엔티티
Expand Down Expand Up @@ -119,20 +121,17 @@ public KakaoUserInfo getKakaoUserInfo(String accessToken) {
// 3. 사용자 정보를 DB에서 조회하고, 가입되지 않은 사용자라면 DB에 저장 후 해당 사용자 반환
public Users login(KakaoUserInfo kakaoUserInfo) {
Long kakaoId = kakaoUserInfo.getId(); // 사용자의 카카오 아이디 불러오기
Users findUser = userService.findUserKaKaoId(kakaoId); // 카카오 아이디로 유저 조회
Users findUser = userRepository.findByKakaoId(kakaoId); // 카카오 아이디로 유저 조회

// 가입되지 않은 사용자라면
if (findUser == null) {
// DB에 새로 저장
userService.saveUser(kakaoUserInfo);
// 사용자 재조회
findUser = userService.findUserKaKaoId(kakaoId);
// 최초 사용자로 설정
findUser.setFirst(true);
System.out.println("findUser.isFirst() = " + findUser.isFirst());
findUser = userRepository.findByKakaoId(kakaoId);
} else { // 이미 가입한 사용자라면
if (findUser.isFirst()) {
findUser.setFirst(false);
findUser.updateFirst();
}
}
return findUser;
Expand Down