๋ค์ ๋ด์ฉ์ ๊ตฌ๊ธ ๋ก๊ทธ์ธ API ๋ฅผ ์ฐ๋ํ๋ฉด์ ๋ฐ์ํ๋ ๋ฌธ์ ์ ๋ฐ ํด๊ฒฐ ๋ฐฉ๋ฒ์ ๋ณต๊ธฐํ ๋ด์ฉ์ ๋๋ค.
๊ธฐ์กด ์ฐ๋ ๊ธ : lucete-stellae.tistory.com/5?category=905216
1. ๋ก๊ทธ์ธ๊ณผ ํ์๊ฐ์ ๋ถ๋ฆฌ ์ํฉ์์์ ๊ตฌ๊ธ ๋ก๊ทธ์ธ ๋ฒํผ
์ ๊ธ๊ณผ ๊ตฌ๊ธ API ๋ฌธ์์์ ์ ๊ณตํ๋ ์์ ๋ฅผ ๊ตฌ์ฑํ๋ ๊ฒฝ์ฐ, ํ ํ์ด์ง์ ๋ก๊ทธ์ธ ๋ฒํผ ๋ฐ ๋ก๊ทธ์์ ๋ฒํผ์ด ๋์์ ์กด์ฌํ๊ฒ ๋ฉ๋๋ค. ์ด ๊ฒฝ์ฐ์ ์์ ๋ฅผ ์ค์ตํ๊ธฐ์๋ ์ข์ง๋ง, ์ด๋ ํ๊ท ์ ์ธ ํํ์ด์ง ๋ก๊ทธ์ธ ๋ฐ ํ์๊ฐ์ ์์ SNS ์ฐ๋ํ๋ ๊ฒฝ์ฐ์๋ ๋ถ์ ํฉํ๋ค๊ณ ๋ณผ ์ ์์ต๋๋ค.
๋ฐ๋ผ์, ์ ๊ฐ ์ฐ๋ ์์ ์ค์ต ํ ์ค์ ์ฐ๋ํ๋ ๊ณผ์ ์์ ๊ตฌ๊ธ๋ก ๋ก๊ทธ์ธํ๋ ๋ถ๋ถ๊ณผ ํ์๊ฐ์ ํ๋ ๋ถ๋ถ 2๊ฐ์ ๊ตฌ๊ธ ๋ก๊ทธ์ธ ์ฐ๋ ๋ฒํผ์ด ํ์ํ์ต๋๋ค.
- ๋ก๊ทธ์ธ : ๊ตฌ๊ธ ๋ก๊ทธ์ธ ํธ์ถ -> ๋ก๊ทธ์ธ ์ ๋ณด ๋ฐ ์๋น์ค ๋ด ํ์์ ๋ณด ๋น๊ต -> ์ ๋ณด ์กด์ฌ ์, ๋ก๊ทธ์ธ ์๋ ๊ฒฝ์ฐ ๋น๋ก๊ทธ์ธ.
- ํ์๊ฐ์ : ๊ตฌ๊ธ ๋ก๊ทธ์ธ ํธ์ถ -> ๋ก๊ทธ์ธ ์ ๋ณด์์ ๊ตฌ๊ธ ์ ์ ์ ๋ณด๋ฅผ ๊ฐ์ ธ์ ์์์ฐฝ์ ์ธํ -> ์๋น์ค ํ์๊ฐ์ ์งํ.
์ ํ๋ก์ธ์ค๋ฅผ ๊ตฌ์ฑํ๋ ๋ฐ์ ์์ด์ ํ๋ฒ ๋ก๊ทธ์ธ์ ํธ์ถ ํ๋ฉด, ๋ก๊ทธ์ธ์ด ๋ ๊ฒ์ผ๋ก ํ๋จ๋์ด
function signOut () {
var auth2 = gapi.auth2.getAuthInstance();
auth2.signOut().then( function () {
console.log( 'User signed out.' );
} );
};
์ ๋ก๊ทธ ์์์ ํธ์ถํ๊ธฐ ์ ๊น์ง๋ ์๋์ผ๋ก ๋ก๊ทธ์ธ ๋๋ ๋ฌธ์ ๊ฐ ์์์ต๋๋ค.
์ด ๋ฌธ์ ๋ฅผ ์ฐพ์๋ณธ ๊ฒฐ๊ณผ,
function onSignIn(googleUser) {
var profile = googleUser.getBasicProfile();
console.log('ID: ' + profile.getId()); // Do not send to your backend! Use an ID token instead.
console.log('Name: ' + profile.getName());
console.log('Image URL: ' + profile.getImageUrl());
console.log('Email: ' + profile.getEmail()); // This is null if the 'email' scope is not present.
}
<div class="g-signin2" data-onsuccess="onSignIn"></div>
์ด๋ฐ ์์ผ๋ก, div ํ๊ทธ ์์ ๋ฒํผ์ด ์์ฑ๋๊ฒ ๋๋๋ฐ, data-onsuccess ์ onSignIn ์ด ๋งคํ๋์ด ์์ด, ๋ฒํผ์ด ๋ ๋๋ง ๋๊ณ ๋์, ๋ฒํผ์ ํ๋ฒ์ด๋ผ๋ ํด๋ฆญํ๊ณ ๋ก๊ทธ์์์ ํ์ง ์๋ ๊ฒฝ์ฐ, ๋ค์ ์ ์ํด๋ ํด๋น ํจ์๋ฅผ ๋ฌด์กฐ๊ฑด ์คํ๋๊ฒ๋ ๋์ด์์์ต๋๋ค.
๋ฐ๋ผ์ ๋ค์๊ณผ ๊ฐ์ด, ๋ก๊ทธ์ธ ์ํฉ์์๋ ๋ก๊ทธ์ธ ํผ์ ํ์ฐ๊ณ , ๋ก๊ทธ์์์ ์์ผฐ์ต๋๋ค.
function onSuccess ( googleUser ) {
console.log( googleUser );
var profile = googleUser.getBasicProfile();
signOut();
var $form = $( "#snsLoginForm" );
$form.find( "input[name='id']" ).val( profile.getEmail() );
$form.find( "input[name='snsType']" ).val( "google" );
$form.attr( "action" ,"/comm/member/googleLogin.do" );
$form.submit();
}
ํ์ ๊ฐ์ ์ํฉ์์๋ ํ์ํ ๋ถ๋ถ์ ์คํ ํ, ๋ก๊ทธ์์์ ์ํต๋๋ค.
function onSuccess ( googleUser ) {
var profile = googleUser.getBasicProfile();
// ๊ตฌ๊ธ ๊ฐ์
๋ฒํผ ํด๋ฆญ ์, ๊ฐ์
์ ๋ณด ์ฒดํฌ
$.ajax( {
"url" : "/comm/member/snsSignChk.do" ,
"dataType" : "json" ,
"data" : {
"id" : profile.getEmail() ,
"password" : profile.getEmail() ,
"snsType" : "google"
} ,
"success" : function ( data ) {
console.log( data );
if ( data.result > 0 ) {
$.alert2( "์ด๋ฏธ ๊ฐ์
๋ ๊ณ์ ์
๋๋ค." ,{
"close" : function () {
signOut();
}
} );
signOut();
return false;
} else {
$( "#removeSection1" ).remove();
$( "#removeSection2" ).remove();
$( "#removeSection3" ).remove();
var $form = document.signupForm;
$form.snsLoginYn.value = "Y";
$form.email.value = profile.getEmail();
$form.snsType.value = "google";
$( "#snsLoginId" ).val( profile.getEmail() );
$( "#snsLoginPw" ).val( profile.getEmail() );
signOut();
}
}
} );
}
์ด๋ ๊ฒ ๋๋ฉด ์ค๋ณต ํธ์ถ ๋ฌธ์ ๋ฟ๋ง ์๋๋ผ ๋ก๊ทธ์ธ ํ, ๋ก๊ทธ์์๋ ์๋น์ค์์๋ง ๋ก๊ทธ์์ ์ํค๋ฉด ๋๋๊ฒ ๋ฉ๋๋ค.
2. gapi Cannot read property 'style' of null
- ์ ์ผ ์ฝ์ง ๋ง์ดํ ๋ฌธ์ ๋ก์จ ๋ฒํผ์ 2๊ฐ์ด์ ๋ง๋๋ ๋์์ ์์ ์ํ๋ ๋ฌธ์ ๊ฐ ์๊ฒผ์ต๋๋ค. ์ฌ๊ธฐ์ ๊ธฐ๋ณธ์ ์ผ๋ก ๊น๊ณ ๊ฐ์ผํ ๋ด์ฉ์ ๊ตฌ๊ธ ๋ก๊ทธ์ธ ์ฐ๋ ๋ฒํผ์ ์ ์ฉ๋๋ ๋ฒํผ ๋ ๋๋ง๋ช ์ด ๊ฒน์น๋ฉด ์๋๋ค๋ ๊ฒ์ ๋๋ค.
๊ด๋ จํ ๋ด์ฉ์ผ๋ก๋ ๋ค์์ ์ฐธ๊ณ ํ์ต๋๋ค.
github.com/google/google-api-javascript-client/issues/281
Cannot read property 'style' of null · Issue #281 · google/google-api-javascript-client
First, I already try to find solution on SO, but I think this is a bug. I am using Angular2 application. (For backend I use ASP.NET core 1.1.) I also create SignIn button following official tutoria...
github.com
๊ธฐ๋ณธ์ผ๋ก ์ ๊ณต๋๋ "<div class="g-signin2" data-onsuccess="onSignIn"></div>" ๋ง ์ฌ์ฉํ๋ ๊ฒฝ์ฐ, ํ๋๋ง ์ฌ์ฉํด์ผ ํ๋๋ฐ ๋์ผํ ํด๋์ค๋ช ์ ๊ฐ๋ ๋ฒํผ์ ํ๋ ๋ ์ถ๊ฐํ๋ ๊ฒฝ์ฐ ๋จนํต์ด ๋ฉ๋๋ค.
๋ฐ๋ผ์, ํด๋น ๋ฒํผ๋ค์ ์ปค์คํ ๋ ๋๋ง์ ํด์ผํฉ๋๋ค.
์ปค์คํ ๋ ๋๋ง์ ๊ตฌ๊ธ ๋ฌธ์์ ๋ค์์ ์ฐธ๊ณ ํด์ฃผ์ธ์.
developers.google.com/identity/sign-in/web/build-button
Building a custom Google Sign-In button | Google Sign-In for Websites
Customizing the automatically rendered sign-in button (recommended) To create a Google Sign-In button with custom settings, add an element to contain the sign-in button to your sign-in page, write a function that calls signin2.render() with your style and
developers.google.com
์๋๋ ์ ๊ฐ ์ฌ์ฉํ ์ฝ๋์ ๋๋ค.
- ๋ก๊ทธ์ธ ํ์ด์ง
<script src="https://apis.google.com/js/platform.js?onload=renderButtonLogin" async defer></script>
<meta name="google-signin-client_id" content="380583811608-sqj8rb44q1sd2vfac8sq75bp7af966pt.apps.googleusercontent.com">
<script type="text/javascript">
function onSuccess ( googleUser ) {
console.log( googleUser );
var profile = googleUser.getBasicProfile();
signOut();
var $form = $( "#snsLoginForm" );
$form.find( "input[name='id']" ).val( profile.getEmail() );
$form.find( "input[name='snsType']" ).val( "google" );
$form.attr( "action" ,"/comm/member/googleLogin.do" );
$form.submit();
}
function onFailure ( error ) {
console.log( error );
}
function renderButtonLogin () {
gapi.signin2.render( 'my-signin1' ,{
'scope' : 'profile email' ,
'width' : 100 ,
'height' : 50 ,
'longtitle' : true ,
'theme' : 'dark' ,
'onsuccess' : onSuccess ,
'onfailure' : onFailure
} );
}
function signOut () {
var auth2 = gapi.auth2.getAuthInstance();
auth2.signOut().then( function () {
console.log( 'User signed out.' );
} );
};
</script>
<!-- ๋ก๊ทธ์ธ ๋ฒํผ -->
<div id="my-signin1"></div>
<!-- ๋ก๊ทธ์ธ ๋ฒํผ -->
- ํ์๊ฐ์ ํ์ด์ง
<script src="https://apis.google.com/js/platform.js?onload=renderButton" async defer></script>
<meta name="google-signin-client_id" content="380583811608-sqj8rb44q1sd2vfac8sq75bp7af966pt.apps.googleusercontent.com">
<script type="text/javascript">
function onSuccess ( googleUser ) {
var profile = googleUser.getBasicProfile();
// ๊ตฌ๊ธ ๊ฐ์
๋ฒํผ ํด๋ฆญ ์, ๊ฐ์
์ ๋ณด ์ฒดํฌ
$.ajax( {
"url" : "/comm/member/snsSignChk.do" ,
"dataType" : "json" ,
"data" : {
"id" : profile.getEmail() ,
"password" : profile.getEmail() ,
"snsType" : "google"
} ,
"success" : function ( data ) {
console.log( data );
if ( data.result > 0 ) {
$.alert2( "์ด๋ฏธ ๊ฐ์
๋ ๊ณ์ ์
๋๋ค." ,{
"close" : function () {
signOut();
}
} );
signOut();
return false;
} else {
$( "#removeSection1" ).remove();
$( "#removeSection2" ).remove();
$( "#removeSection3" ).remove();
var $form = document.signupForm;
$form.snsLoginYn.value = "Y";
$form.email.value = profile.getEmail();
$form.snsType.value = "google";
$( "#snsLoginId" ).val( profile.getEmail() );
$( "#snsLoginPw" ).val( profile.getEmail() );
signOut();
}
}
} );
}
function onFailure ( error ) {
console.log( error );
}
function renderButton () {
gapi.signin2.render( 'my-signin2' ,{
'scope' : 'profile email' ,
'width' : 100 ,
'height' : 50 ,
'longtitle' : true ,
'theme' : 'dark' ,
'onsuccess' : onSuccess ,
'onfailure' : onFailure
} );
}
</script>
<!-- ํ์๊ฐ์
๋ฒํผ -->
<div id="my-signin2"></div>
<!-- ํ์๊ฐ์
๋ฒํผ -->
๋๊ธ