Skip to content

Instantly share code, notes, and snippets.

@mprkatti
Last active December 15, 2020 10:51
Show Gist options
  • Save mprkatti/cf72faa728ad7ddc0db3358f3b7cc9a9 to your computer and use it in GitHub Desktop.
Save mprkatti/cf72faa728ad7ddc0db3358f3b7cc9a9 to your computer and use it in GitHub Desktop.
import React, { Component } from 'react'
export default class GoogleAuth extends Component {
state = { isAuthenticated: null };
componentDidMount() {
window.gapi.load('client:auth2', () => {
window.gapi.client.init({
clientId: 'abcslkdjslkd.apps.googleusercontent.com',
scope: 'email'
}).then(() => {
this.auth = window.gapi.auth2.getAuthInstance();
this.setState({ isAuthenticated: this.auth.isSignedIn.get() });
this.auth.isSignedIn.listen(this.onAuthChange);
});
});
}
onAuthChange = () => {
this.setState({ isAuthenticated: this.auth.isSignedIn.get() });
this.props.setSignIn(this.state.isAuthenticated);
}
render() {
if (!this.props.intentToSign && this.auth) {
console.log('Signing out !');
this.auth.signOut();
}
if (this.auth && this.auth.isSignedIn.get()) {
return <></>
}
if (this.props.intentToSign && this.auth) {
this.auth.signIn();
}
return (
<>
</>
)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment