Skip to content

Instantly share code, notes, and snippets.

@ivanvs
Created September 16, 2017 23:22
Show Gist options
  • Select an option

  • Save ivanvs/584c6e84362a03eb54c59ccee2f10700 to your computer and use it in GitHub Desktop.

Select an option

Save ivanvs/584c6e84362a03eb54c59ccee2f10700 to your computer and use it in GitHub Desktop.
React example how to do authenitcation with Twitter
onSuccess = (response) => {
const token = response.headers.get('x-auth-token');
response.json().then(user => {
if (token) {
this.setState({isAuthenticated: true, user: user, token: token});
}
});
};
onFailed = (error) => {
alert(error);
};
render() {
let content = !!this.state.isAuthenticated ?
(
<div>
<p>Authenticated</p>
<div>
{this.state.user.email}
</div>
<div>
<button onClick={this.logout} className="button" >
Log out
</button>
</div>
</div>
) :
(
<TwitterLogin loginUrl="http://localhost:4000/api/v1/auth/twitter"
onFailure={this.onFailed} onSuccess={this.onSuccess}
requestTokenUrl="http://localhost:4000/api/v1/auth/twitter/reverse"/>
);
return (
<div className="App">
{content}
</div>
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment