forked from anthonyjgrove/react-google-login
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request anthonyjgrove#110 from anthonyjgrove/logout
Added Logout button
- Loading branch information
Showing
7 changed files
with
546 additions
and
219 deletions.
There are no files selected for viewing
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,182 @@ | ||
import React, { Component } from 'react' | ||
import PropTypes from 'prop-types' | ||
|
||
class GoogleLogin extends Component { | ||
constructor(props) { | ||
super(props) | ||
this.signIn = this.signIn.bind(this) | ||
this.state = { | ||
disabled: true | ||
} | ||
} | ||
componentDidMount() { | ||
const { clientId, cookiePolicy, loginHint, hostedDomain, autoLoad, isSignedIn, fetchBasicProfile, redirectUri, discoveryDocs, onFailure, uxMode, scope } = this.props | ||
; ((d, s, id, cb) => { | ||
const element = d.getElementsByTagName(s)[0] | ||
const fjs = element | ||
let js = element | ||
js = d.createElement(s) | ||
js.id = id | ||
js.src = '//apis.google.com/js/client:platform.js' | ||
fjs.parentNode.insertBefore(js, fjs) | ||
js.onload = cb | ||
})(document, 'script', 'google-login', () => { | ||
const params = { | ||
client_id: clientId, | ||
cookie_policy: cookiePolicy, | ||
login_hint: loginHint, | ||
hosted_domain: hostedDomain, | ||
fetch_basic_profile: fetchBasicProfile, | ||
discoveryDocs, | ||
ux_mode: uxMode, | ||
redirect_uri: redirectUri, | ||
scope | ||
} | ||
window.gapi.load('auth2', () => { | ||
this.setState({ | ||
disabled: false | ||
}) | ||
if (!window.gapi.auth2.getAuthInstance()) { | ||
window.gapi.auth2.init(params).then( | ||
res => { | ||
if (isSignedIn && res.isSignedIn.get()) { | ||
this._handleSigninSuccess(res.currentUser.get()) | ||
} | ||
}, | ||
err => onFailure(err) | ||
) | ||
} | ||
if (autoLoad) { | ||
this.signIn() | ||
} | ||
}) | ||
}) | ||
} | ||
signIn(e) { | ||
if (e) { | ||
e.preventDefault() // to prevent submit if used within form | ||
} | ||
if (!this.state.disabled) { | ||
const auth2 = window.gapi.auth2.getAuthInstance() | ||
const { onSuccess, onRequest, onFailure, prompt, responseType } = this.props | ||
const options = { | ||
prompt | ||
} | ||
onRequest() | ||
if (responseType === 'code') { | ||
auth2.grantOfflineAccess(options).then(res => onSuccess(res), err => onFailure(err)) | ||
} else { | ||
auth2.signIn(options).then(res => this._handleSigninSuccess(res), err => onFailure(err)) | ||
} | ||
} | ||
} | ||
_handleSigninSuccess(res) { | ||
/* | ||
offer renamed response keys to names that match use | ||
*/ | ||
const basicProfile = res.getBasicProfile() | ||
const authResponse = res.getAuthResponse() | ||
res.googleId = basicProfile.getId() | ||
res.tokenObj = authResponse | ||
res.tokenId = authResponse.id_token | ||
res.accessToken = authResponse.access_token | ||
res.profileObj = { | ||
googleId: basicProfile.getId(), | ||
imageUrl: basicProfile.getImageUrl(), | ||
email: basicProfile.getEmail(), | ||
name: basicProfile.getName(), | ||
givenName: basicProfile.getGivenName(), | ||
familyName: basicProfile.getFamilyName() | ||
} | ||
this.props.onSuccess(res) | ||
} | ||
|
||
render() { | ||
const { tag, type, style, className, disabledStyle, buttonText, children } = this.props | ||
const disabled = this.state.disabled || this.props.disabled | ||
const initialStyle = { | ||
display: 'inline-block', | ||
background: '#d14836', | ||
color: '#fff', | ||
width: 190, | ||
paddingTop: 10, | ||
paddingBottom: 10, | ||
borderRadius: 2, | ||
border: '1px solid transparent', | ||
fontSize: 16, | ||
fontWeight: 'bold', | ||
fontFamily: 'Roboto' | ||
} | ||
const styleProp = (() => { | ||
if (style) { | ||
return style | ||
} else if (className && !style) { | ||
return {} | ||
} | ||
return initialStyle | ||
})() | ||
const defaultStyle = (() => { | ||
if (disabled) { | ||
return Object.assign({}, styleProp, disabledStyle) | ||
} | ||
return styleProp | ||
})() | ||
const googleLoginButton = React.createElement( | ||
tag, | ||
{ | ||
onClick: this.signIn, | ||
style: defaultStyle, | ||
type, | ||
disabled, | ||
className | ||
}, | ||
children || buttonText | ||
) | ||
return googleLoginButton | ||
} | ||
} | ||
|
||
GoogleLogin.propTypes = { | ||
onSuccess: PropTypes.func.isRequired, | ||
onFailure: PropTypes.func.isRequired, | ||
clientId: PropTypes.string.isRequired, | ||
onRequest: PropTypes.func, | ||
buttonText: PropTypes.string, | ||
scope: PropTypes.string, | ||
className: PropTypes.string, | ||
redirectUri: PropTypes.string, | ||
cookiePolicy: PropTypes.string, | ||
loginHint: PropTypes.string, | ||
hostedDomain: PropTypes.string, | ||
children: PropTypes.node, | ||
style: PropTypes.object, | ||
disabledStyle: PropTypes.object, | ||
fetchBasicProfile: PropTypes.bool, | ||
prompt: PropTypes.string, | ||
tag: PropTypes.string, | ||
autoLoad: PropTypes.bool, | ||
disabled: PropTypes.bool, | ||
discoveryDocs: PropTypes.array, | ||
uxMode: PropTypes.string, | ||
isSignedIn: PropTypes.bool, | ||
responseType: PropTypes.string, | ||
type: PropTypes.string | ||
} | ||
|
||
GoogleLogin.defaultProps = { | ||
type: 'button', | ||
tag: 'button', | ||
buttonText: 'Login with Google', | ||
scope: 'profile email', | ||
prompt: '', | ||
cookiePolicy: 'single_host_origin', | ||
fetchBasicProfile: true, | ||
isSignedIn: false, | ||
uxMode: 'popup', | ||
disabledStyle: { | ||
opacity: 0.6 | ||
}, | ||
onRequest: () => { } | ||
} | ||
|
||
export default GoogleLogin |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
import React, { Component } from 'react' | ||
import PropTypes from 'prop-types' | ||
|
||
class GoogleLogout extends Component { | ||
constructor(props) { | ||
super(props) | ||
this.state = { | ||
disabled: true, | ||
} | ||
this.signOut = this.signOut.bind(this) | ||
} | ||
componentDidMount() { | ||
((d, s, id, cb) => { | ||
const element = d.getElementsByTagName(s)[0] | ||
const fjs = element | ||
let js = element | ||
js = d.createElement(s) | ||
js.id = id | ||
js.src = '//apis.google.com/js/client:platform.js' | ||
fjs.parentNode.insertBefore(js, fjs) | ||
js.onload = cb | ||
})(document, 'script', 'google-login', () => { | ||
window.gapi.load('auth2', () => { | ||
this.setState({ | ||
disabled: false, | ||
}) | ||
}) | ||
}) | ||
} | ||
|
||
signOut() { | ||
const auth2 = window.gapi.auth2.getAuthInstance() | ||
if (auth2 != null) { | ||
auth2.signOut().then(() => { | ||
this.props.onLogoutSuccess() | ||
}) | ||
} | ||
} | ||
|
||
render() { | ||
const { tag, style, className, disabledStyle, buttonText, children } = this.props | ||
const disabled = this.state.disabled || this.props.disabled | ||
const initialStyle = { | ||
display: 'inline-block', | ||
background: '#d14836', | ||
color: '#fff', | ||
width: 190, | ||
paddingTop: 10, | ||
paddingBottom: 10, | ||
borderRadius: 2, | ||
border: '1px solid transparent', | ||
fontSize: 16, | ||
fontWeight: 'bold', | ||
fontFamily: 'Roboto', | ||
} | ||
const styleProp = (() => { | ||
if (style) { | ||
return style | ||
} else if (className && !style) { | ||
return {} | ||
} | ||
return initialStyle | ||
})() | ||
const defaultStyle = (() => { | ||
if (disabled) { | ||
return Object.assign({}, styleProp, disabledStyle) | ||
} | ||
return styleProp | ||
})() | ||
const googleLoginButton = React.createElement( | ||
tag, { | ||
onClick: this.signOut, | ||
style: defaultStyle, | ||
disabled, | ||
className, | ||
}, children || buttonText | ||
) | ||
return googleLoginButton | ||
} | ||
} | ||
|
||
GoogleLogout.propTypes = { | ||
buttonText: PropTypes.string, | ||
className: PropTypes.string, | ||
children: PropTypes.node, | ||
style: PropTypes.object, | ||
disabledStyle: PropTypes.object, | ||
disabled: PropTypes.bool, | ||
} | ||
|
||
GoogleLogout.defaultProps = { | ||
tag: 'button', | ||
buttonText: 'Logout', | ||
responseType: 'permission', | ||
disabledStyle: { | ||
opacity: 0.6, | ||
}, | ||
onRequest: () => { }, | ||
} | ||
|
||
export default GoogleLogout |
Oops, something went wrong.