Skip to content

Instantly share code, notes, and snippets.

@kwangheum
Last active January 10, 2019 09:03
Show Gist options
  • Save kwangheum/b036f736e980de9140510a8f8b669432 to your computer and use it in GitHub Desktop.
Save kwangheum/b036f736e980de9140510a8f8b669432 to your computer and use it in GitHub Desktop.
구글 로그인 연동
<!doctype html>
<html>
<head>
<title>구글 로그인</title>
<script src="http://code.jquery.com/jquery-1.11.2.min.js"></script>
<script src="https://apis.google.com/js/api:client.js"></script>
<script>
gapi.load('auth2', function() {
gapi.auth2.init({
client_id: '위에서 발급받은 클라이언트 ID를 넣어주세요 ',
cookiepolicy: 'single_host_origin',
scope: 'profile email'
}).then(() => {
var auth2 = gapi.auth2.getAuthInstance();
function createSignInButton() {
$("#profile").empty();
var btn = $("<a/>", {
"text": "로그인"
});
$("#buttons").empty().append(btn);
signIn(btn[0]);
}
function signIn(element) {
auth2.attachClickHandler(element, {
scope: 'profile email'
}, function(googleUser) {
button();
}, function(error) {
alert(JSON.stringify(error, undefined, 2));
});
}
function button() {
if (auth2.isSignedIn.get()) {
$("#profile").append($("<img/>", {
"src": auth2.currentUser.get().getBasicProfile().getImageUrl()
}));
$("#buttons").empty().append($("<a/>", {
"text": "로그아웃"
}).click(function() {
auth2.signOut();
createSignInButton();
}));
} else {
createSignInButton();
}
}
button();
});
});
</script>
</head>
<body>
<div id="buttons"></div>
<div id="profile"></div>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment