Skip to content

Commit

Permalink
Merge pull request #60 from yanghun0070/feature/kakaologin
Browse files Browse the repository at this point in the history
Add Kakao Login Function(#59)
  • Loading branch information
yanghun0070 committed May 23, 2021
2 parents 83fe272 + 9d7f1e7 commit eb00670
Show file tree
Hide file tree
Showing 8 changed files with 68 additions and 9 deletions.
Binary file added dayco-app/src/components/img/kakao-logo.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
11 changes: 10 additions & 1 deletion dayco-app/src/components/user/login/Login.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ import { withRouter } from "react-router";
import googleLogo from '../../img/google-logo.png';
import githubLogo from '../../img/github-logo.png';
import naverLogo from '../../img/naver-logo.png';
import { GOOGLE_AUTH_URL, GITHUB_AUTH_URL, NAVER_AUTH_URL, TISTORY_AUTH_URL } from '../../../constants';
import kakaoLogo from '../../img/kakao-logo.png';
import { GOOGLE_AUTH_URL, GITHUB_AUTH_URL, NAVER_AUTH_URL, KAKAO_AUTH_URL } from '../../../constants';

class Login extends Component {

Expand Down Expand Up @@ -94,6 +95,14 @@ class Login extends Component {
&nbsp;Log in with Naver</Button>
</Col>
</Form.Group>
<Form.Group as={Row}>
<Col sm="2"></Col>
<Col sm="8">
<Button size="sm" block variant="outline-info" href={KAKAO_AUTH_URL}>
<Image src={kakaoLogo} alt="Kakao" thumbnail width={26} height={26}/>
&nbsp;Log in with Kakao</Button>
</Col>
</Form.Group>
</Form>
{(this.props.user.authenticated === true
&& this.props.user.logon === true) ?
Expand Down
2 changes: 2 additions & 0 deletions dayco-app/src/constants/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ export const OAUTH2_REDIRECT_URI = 'http://localhost:3000/oauth2/redirect'
export const GOOGLE_AUTH_URL = API_SOCIAL_URL + '/oauth2/authorization/google?redirect_uri=' + OAUTH2_REDIRECT_URI;
export const GITHUB_AUTH_URL = API_SOCIAL_URL + '/oauth2/authorization/github?redirect_uri=' + OAUTH2_REDIRECT_URI;
export const NAVER_AUTH_URL = API_SOCIAL_URL + '/oauth2/authorization/naver?redirect_uri=' + OAUTH2_REDIRECT_URI;
export const KAKAO_AUTH_URL = API_SOCIAL_URL + '/oauth2/authorization/kakao?redirect_uri=' + OAUTH2_REDIRECT_URI;


// 최대 보여질 Posts 댓글 개수
export const MAX_SHOW_POST_COMMENT = 5;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import io.github.dayco.uaa.auth.dto.OAuthAttributes;
import io.github.dayco.uaa.auth.dto.SessionUser;
import io.github.dayco.uaa.manager.domain.UserAuthorization;
import io.github.dayco.uaa.social.domain.SocialLogin;
import io.github.dayco.uaa.user.domain.User;
import io.github.dayco.uaa.user.infra.UserJpaRepository;

Expand Down Expand Up @@ -52,8 +53,9 @@ public OAuth2User loadUser(OAuth2UserRequest userRequest) throws OAuth2Authentic

private User saveOrUpdate(String registrationId, OAuthAttributes attributes) {
User user;
// Github 일 경우, Email 이 없다.
if("github".equals(registrationId)) {
// Github 또는 kakao 일 경우, Email 이 없다.
if(SocialLogin.GITHUB.equals(registrationId)
|| SocialLogin.KAKAO.equals(registrationId)) {
User changedUser = new User(attributes.getName(), "social", attributes.getPicture());
changedUser.addUserAuthorization(new UserAuthorization(changedUser, "USER"));
user = userJpaRepository.findByUserId(attributes.getName())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

import io.github.dayco.uaa.auth.exception.InvalidJwtAuthenticationException;

import io.github.dayco.uaa.social.domain.SocialLogin;
import io.jsonwebtoken.Claims;
import io.jsonwebtoken.Jws;
import io.jsonwebtoken.JwtException;
Expand All @@ -45,12 +46,16 @@ protected void init() {
}

public String createToken(DefaultOAuth2User oAuth2User, String registrationId) {
Map<String, Object> userAttributes = oAuth2User.getAttributes();
Map<String, Object> userAttributes = oAuth2User.getAttributes();

String subject = "";
if("github".equals(registrationId)) {
if (SocialLogin.GITHUB.equals(registrationId)) {
subject = (String) userAttributes.get("login");
} else { //Google, Naver 는 ID 대신 이름으로 대체
} else if(SocialLogin.KAKAO.equals(registrationId)) {
Map<String, Object> kakaoAccount = (Map<String, Object>) userAttributes.get("kakao_account");
Map<String, Object> profile = (Map<String, Object>) kakaoAccount.get("profile");
subject = (String) profile.get("nickname");
}else { //Google, Naver 는 ID 대신 이름으로 대체
subject = (String) userAttributes.get("name");
}
Date now = new Date();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import java.util.Map;

import io.github.dayco.uaa.social.domain.SocialLogin;
import lombok.Builder;
import lombok.Getter;

Expand All @@ -25,14 +26,16 @@ public OAuthAttributes(Map<String, Object> attributes,
}

public static OAuthAttributes of(String registrationId, String userNameAttributeName, Map<String, Object> attributes) {
if("naver".equals(registrationId)) {
if(SocialLogin.NAVER.name().equals(registrationId)) {
return ofNaver("id", attributes);
}
OAuthAttributes oAuthAttributes = null;
if("google".equals(registrationId)) {
if(SocialLogin.GOOGLE.name().equals(registrationId)) {
oAuthAttributes = ofGoogle(userNameAttributeName, attributes);
} else if("github".equals(registrationId)) {
} else if(SocialLogin.GITHUB.name().equals(registrationId)) {
oAuthAttributes = ofGithub(userNameAttributeName, attributes);
} else if(SocialLogin.KAKAO.name().equals(registrationId)) {
oAuthAttributes = ofKakao(userNameAttributeName, attributes);
}
return oAuthAttributes;
}
Expand Down Expand Up @@ -66,4 +69,15 @@ private static OAuthAttributes ofNaver(String userNameAttributeName, Map<String,
.nameAttributeKey(userNameAttributeName)
.build();
}

private static OAuthAttributes ofKakao(String userNameAttributeName, Map<String, Object> attributes) {
Map<String, Object> kakaoAccount = (Map<String, Object>) attributes.get("kakao_account");
Map<String, Object> profile = (Map<String, Object>) kakaoAccount.get("profile");
return OAuthAttributes.builder()
.name((String) profile.get("nickname"))
.picture((String) profile.get("thumbnail_image_url"))
.attributes(attributes)
.nameAttributeKey(userNameAttributeName)
.build();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package io.github.dayco.uaa.social.domain;

public enum SocialLogin {
NAVER("naver"),
GOOGLE("google"),
GITHUB("github"),
KAKAO("kakao");

private String name;

SocialLogin(String name) {
this.name = name;
}

}
12 changes: 12 additions & 0 deletions dayco-uaa/src/main/resources/application-oauth.yml.template
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,23 @@ spring:
clientSecret:
redirectUri: "{baseUrl}/oauth2/callback/{registrationId}"
scope: user:email, read:user
kakao:
clientId:
clientSecret:
redirectUri: "{baseUrl}/oauth2/callback/{registrationId}"
scope: profile
authorizationGrantType: authorization_code
provider:
naver:
authorizationUri: https://nid.naver.com/oauth2.0/authorize
tokenUri: https://nid.naver.com/oauth2.0/token
userInfoUri: https://openapi.naver.com/v1/nid/me
userNameAttribute: response
kakao:
authorizationUri: https://kauth.kakao.com/oauth/authorize
tokenUri: https://kauth.kakao.com/oauth/token
userInfoUri: https://kapi.kakao.com/v2/user/me
userInfoAuthenticationMethod: post
userNameAttribute: id


0 comments on commit eb00670

Please sign in to comment.