Created
September 16, 2017 23:22
-
-
Save ivanvs/584c6e84362a03eb54c59ccee2f10700 to your computer and use it in GitHub Desktop.
React example how to do authenitcation with Twitter
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
| 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