Last active
May 12, 2018 13:43
-
-
Save kariyayo/4d98b3962b7e6ead6ca321dd6f1ab6e9 to your computer and use it in GitHub Desktop.
Google SignIn + React (http://blog.bati11.info/entry/2018/05/12/133538)
This file contains hidden or 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
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; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
$ create-react-app
してから、src/App.js
を編集。