Skip to content

Commit

Permalink
Merge pull request #8 from InjeRecipe/#1-Social-Sign-on
Browse files Browse the repository at this point in the history
feat: Kakao, Naver social login
  • Loading branch information
Gdm0714 committed Jan 15, 2024
2 parents e8e9a84 + 82bd549 commit 39f14d2
Show file tree
Hide file tree
Showing 5 changed files with 123 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
import com.example.injerecipe.dto.SocialType;
import com.example.injerecipe.entity.Member;
import com.example.injerecipe.oauth2.userinfo.GoogleOAuth2UserInfo;
import com.example.injerecipe.oauth2.userinfo.KakaoOAuth2UserInfo;
import com.example.injerecipe.oauth2.userinfo.NaverOAuth2UserInfo;
import com.example.injerecipe.oauth2.userinfo.OAuth2UserInfo;
import lombok.Builder;
import lombok.Getter;
Expand All @@ -23,17 +25,23 @@ public OAuthAttributes(String nameAttributeKey, OAuth2UserInfo oauth2UserInfo) {
this.oauth2UserInfo = oauth2UserInfo;
}

/**
* SocialType에 맞는 메소드 호출하여 OAuthAttributes 객체 반환
* 파라미터 : userNameAttributeName -> OAuth2 로그인 시 키(PK)가 되는 값 / attributes : OAuth 서비스의 유저 정보들
* 소셜별 of 메소드(ofGoogle, ofKaKao, ofNaver)들은 각각 소셜 로그인 API에서 제공하는
* 회원의 식별값(id), attributes, nameAttributeKey를 저장 후 build
*/
public static OAuthAttributes of(SocialType socialType, String userNameAttributeName, Map<String, Object> attributes) {

if (socialType == SocialType.NAVER) {
return ofNaver(userNameAttributeName, attributes);
}
if (socialType == SocialType.KAKAO) {
return ofKakao(userNameAttributeName, attributes);
}
return ofGoogle(userNameAttributeName, attributes);
}

private static OAuthAttributes ofKakao(String userNameAttributeName, Map<String, Object> attributes) {
return OAuthAttributes.builder()
.nameAttributeKey(userNameAttributeName)
.oauth2UserInfo(new KakaoOAuth2UserInfo(attributes))
.build();
}

public static OAuthAttributes ofGoogle(String userNameAttributeName, Map<String, Object> attributes) {
return OAuthAttributes.builder()
Expand All @@ -42,6 +50,12 @@ public static OAuthAttributes ofGoogle(String userNameAttributeName, Map<String,
.build();
}

public static OAuthAttributes ofNaver(String userNameAttributeName, Map<String, Object> attributes) {
return OAuthAttributes.builder()
.nameAttributeKey(userNameAttributeName)
.oauth2UserInfo(new NaverOAuth2UserInfo(attributes))
.build();
}

public Member toEntity(SocialType socialType, OAuth2UserInfo oauth2UserInfo) {
return Member.builder()
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package com.example.injerecipe.oauth2.userinfo;

import java.util.Map;

public class KakaoOAuth2UserInfo extends OAuth2UserInfo {

public KakaoOAuth2UserInfo(Map<String, Object> attributes) {
super(attributes);
}

@Override
public String getId() {
return String.valueOf(attributes.get("id"));
}

@Override
public String getNickname() {
Map<String, Object> account = (Map<String, Object>) attributes.get("kakao_account");
Map<String, Object> profile = (Map<String, Object>) account.get("profile");

if (account == null || profile == null) {
return null;
}

return (String) profile.get("nickname");
}

@Override
public String getImageUrl() {
Map<String, Object> account = (Map<String, Object>) attributes.get("kakao_account");
Map<String, Object> profile = (Map<String, Object>) account.get("profile");

if (account == null || profile == null) {
return null;
}

return (String) profile.get("thumbnail_image_url");
}

@Override
public String getEmail() {
return (String) attributes.get("email");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package com.example.injerecipe.oauth2.userinfo;

import java.util.Map;

public class NaverOAuth2UserInfo extends OAuth2UserInfo {

public NaverOAuth2UserInfo(Map<String, Object> attributes) {
super(attributes);
}

@Override
public String getId() {
Map<String, Object> response = (Map<String, Object>) attributes.get("response");

if (response == null) {
return null;
}
return (String) response.get("id");
}

@Override
public String getNickname() {
Map<String, Object> response = (Map<String, Object>) attributes.get("response");

if (response == null) {
return null;
}

return (String) response.get("nickname");
}

@Override
public String getImageUrl() {
Map<String, Object> response = (Map<String, Object>) attributes.get("response");

if (response == null) {
return null;
}

return (String) response.get("profile_image");
}

@Override
public String getEmail() {
return (String) attributes.get("email");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@
public class OAuthService implements OAuth2UserService<OAuth2UserRequest, OAuth2User> {

private final MemberRepository memberRepository;

private static final String NAVER = "naver";
private static final String KAKAO = "kakao";
@Override
public OAuth2User loadUser(OAuth2UserRequest userRequest) throws OAuth2AuthenticationException {
log.info("CustomOAuth2UserService.loadUser() 실행 - OAuth2 로그인 요청 진입");
Expand Down Expand Up @@ -51,6 +54,12 @@ public OAuth2User loadUser(OAuth2UserRequest userRequest) throws OAuth2Authentic
}

private SocialType getSocialType(String registrationId) {
if(NAVER.equals(registrationId)) {
return SocialType.NAVER;
}
if(KAKAO.equals(registrationId)) {
return SocialType.KAKAO;
}
return SocialType.GOOGLE;
}

Expand Down
14 changes: 3 additions & 11 deletions InjeRecipe/src/main/resources/static/index.html
Original file line number Diff line number Diff line change
@@ -1,11 +1,3 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>구글 로그인</title>
</head>
<body>
<a href="/oauth2/authorization/google" class="btn btn-sm btn-success active" role="button">Google Login</a><br>

</body>
</html>
<a href="/oauth2/authorization/kakao">Kakao Login</a><br>
<a href="/oauth2/authorization/google">Google Login</a><br>
<a href="/oauth2/authorization/naver">Naver Login</a><br>

0 comments on commit 39f14d2

Please sign in to comment.