Created
March 24, 2015 07:35
-
-
Save kwangheum/6f9c98d81dcff59f61eb to your computer and use it in GitHub Desktop.
카카오톡 로그인
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!docType html> | |
<html lang="ko"> | |
<head> | |
<title>카카오톡 로그인</title> | |
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/> | |
<script src="http://code.jquery.com/jquery-1.11.2.min.js"></script> | |
<script src="https://developers.kakao.com/sdk/js/kakao.min.js"></script> | |
<script> | |
$(document).ready(function(){ | |
Kakao.init("발급받은 키"); | |
function getKakaotalkUserProfile(){ | |
Kakao.API.request({ | |
url: '/v1/user/me', | |
success: function(res) { | |
$("#kakao-profile").append(res.properties.nickname); | |
$("#kakao-profile").append($("<img/>",{"src":res.properties.profile_image,"alt":res.properties.nickname+"님의 프로필 사진"})); | |
}, | |
fail: function(error) { | |
console.log(error); | |
} | |
}); | |
} | |
function createKakaotalkLogin(){ | |
$("#kakao-logged-group .kakao-logout-btn,#kakao-logged-group .kakao-login-btn").remove(); | |
var loginBtn = $("<a/>",{"class":"kakao-login-btn","text":"로그인"}); | |
loginBtn.click(function(){ | |
Kakao.Auth.login({ | |
persistAccessToken: true, | |
persistRefreshToken: true, | |
success: function(authObj) { | |
getKakaotalkUserProfile(); | |
createKakaotalkLogout(); | |
}, | |
fail: function(err) { | |
console.log(err); | |
} | |
}); | |
}); | |
$("#kakao-logged-group").prepend(loginBtn) | |
} | |
function createKakaotalkLogout(){ | |
$("#kakao-logged-group .kakao-logout-btn,#kakao-logged-group .kakao-login-btn").remove(); | |
var logoutBtn = $("<a/>",{"class":"kakao-logout-btn","text":"로그아웃"}); | |
logoutBtn.click(function(){ | |
Kakao.Auth.logout(); | |
createKakaotalkLogin(); | |
$("#kakao-profile").text(""); | |
}); | |
$("#kakao-logged-group").prepend(logoutBtn); | |
} | |
if(Kakao.Auth.getRefreshToken()!=undefined&&Kakao.Auth.getRefreshToken().replace(/ /gi,"")!=""){ | |
createKakaotalkLogout(); | |
getKakaotalkUserProfile(); | |
}else{ | |
createKakaotalkLogin(); | |
} | |
}); | |
</script> | |
</head> | |
<body> | |
<div id="kakao-logged-group"></div> | |
<div id="kakao-profile"></div> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment