Skip to content

Commit

Permalink
refactor: Modify AuthService (#56)
Browse files Browse the repository at this point in the history
Co-authored-by: Ahyun0326 <[email protected]>
  • Loading branch information
chaeeun-Han and Ahyun0326 committed Apr 4, 2024
1 parent 07ccc1f commit ac3931f
Showing 1 changed file with 6 additions and 7 deletions.
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

0 comments on commit ac3931f

Please sign in to comment.