Skip to content

Instantly share code, notes, and snippets.

@kariyayo
Last active May 12, 2018 13:43
Show Gist options
  • Save kariyayo/4d98b3962b7e6ead6ca321dd6f1ab6e9 to your computer and use it in GitHub Desktop.
Save kariyayo/4d98b3962b7e6ead6ca321dd6f1ab6e9 to your computer and use it in GitHub Desktop.
import React, { Component } from 'react';
class App extends Component {
componentDidMount() {
this.downloadGoogleScript(this.initSignInButton)
}
downloadGoogleScript = (callback) => {
const element = document.getElementsByTagName('script')[0];
const js = document.createElement('script');
js.id = 'google-platform';
js.src = '//apis.google.com/js/platform.js';
js.async = true;
js.defer = true;
element.parentNode.insertBefore(js, element);
js.onload = () => callback(window.gapi);
}
initSignInButton = (gapi) => {
gapi.load('auth2', () => {
gapi.auth2.init({client_id: <CLIENT_ID>})
.then(
(result) => {
gapi.signin2.render('google-signin-button', {
'onsuccess': this.onSignIn,
'onfailure': (err) => console.error(err)
});
},
(err) => console.error(err)
);
})
}
onSignIn = (googleUser) => {
var profile = googleUser.getBasicProfile();
console.log('ID: ' + profile.getId());
console.log('Name: ' + profile.getName());
console.log('Image URL: ' + profile.getImageUrl());
console.log('Email: ' + profile.getEmail());
}
render() {
return (
<div>
ようこそ!
<div id='google-signin-button'></div>
</div>
)
}
}
export default App;
@kariyayo
Copy link
Author

$ create-react-app してから、 src/App.js を編集。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment