Skip to content

Commit

Permalink
feat: Add Kakao user email (#17)
Browse files Browse the repository at this point in the history
  • Loading branch information
Ahyun0326 committed Mar 21, 2024
1 parent 8c821c8 commit 2bd3ebe
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
public class KakaoUserInfo {
private Long id;
private String name;
private String email;
private String profileImageUrl;
private String accessToken;

Expand Down
16 changes: 13 additions & 3 deletions src/main/java/com/beotkkot/qtudy/service/auth/AuthService.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.HttpClientErrorException;
Expand Down Expand Up @@ -96,19 +97,22 @@ public KakaoUserInfo getKakaoUserInfo(String accessToken) {
// ์œ ์ € ์ •๋ณด ์ถ”์ถœ
Long id = element.getAsJsonObject().get("id").getAsLong();
JsonObject properties = element.getAsJsonObject().get("properties").getAsJsonObject();
JsonObject kakaoAccount = element.getAsJsonObject().get("kakao_account").getAsJsonObject();

System.out.println("properties = " + properties);
String name = properties.getAsJsonObject().get("nickname").getAsString();
String profileImageUrl = properties.getAsJsonObject().get("profile_image").getAsString();

String email = kakaoAccount.getAsJsonObject().get("email").getAsString();
// ์นด์นด์˜ค ์œ ์ € ์ •๋ณด ์ƒ์„ฑํ•ด์„œ ๋ฆฌํ„ด
return new KakaoUserInfo(id, name, profileImageUrl, accessToken);
return new KakaoUserInfo(id, name, email, profileImageUrl, accessToken);

} catch (Exception exception) {
log.info("error message: " + exception.getMessage());
return null;
}
}

@Transactional
// 3. ์‚ฌ์šฉ์ž ์ •๋ณด๋ฅผ DB์—์„œ ์กฐํšŒํ•˜๊ณ , ๊ฐ€์ž…๋˜์ง€ ์•Š์€ ์‚ฌ์šฉ์ž๋ผ๋ฉด DB์— ์ €์žฅ ํ›„ ํ•ด๋‹น ์‚ฌ์šฉ์ž ๋ฐ˜ํ™˜
public Users login(KakaoUserInfo kakaoUserInfo) {
Long kakaoId = kakaoUserInfo.getId(); // ์‚ฌ์šฉ์ž์˜ ์นด์นด์˜ค ์•„์ด๋”” ๋ถˆ๋Ÿฌ์˜ค๊ธฐ
Expand All @@ -120,8 +124,14 @@ public Users login(KakaoUserInfo kakaoUserInfo) {
userService.saveUser(kakaoUserInfo);
// ์‚ฌ์šฉ์ž ์žฌ์กฐํšŒ
findUser = userService.findUserKaKaoId(kakaoId);
// ์ตœ์ดˆ ์‚ฌ์šฉ์ž๋กœ ์„ค์ •
findUser.setFirst(true);
System.out.println("findUser.isFirst() = " + findUser.isFirst());
} else { // ์ด๋ฏธ ๊ฐ€์ž…ํ•œ ์‚ฌ์šฉ์ž๋ผ๋ฉด
if (findUser.isFirst()) {
findUser.setFirst(false);
}
}

return findUser;
}

Expand Down

0 comments on commit 2bd3ebe

Please sign in to comment.