forked from anitab-org/anitab-forms-web
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added GitHub Oauth Login and Registration
Added the code to make POST request on backend to send code and recieve access_token. Fixes anitab-org#106
- Loading branch information
1 parent
a1a1f58
commit dfe38a8
Showing
6 changed files
with
181 additions
and
7 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
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,131 @@ | ||
import React, { Component } from 'react'; | ||
import { connect } from 'react-redux'; | ||
import PropTypes from 'prop-types'; | ||
import GitHubLogin from 'react-github-login'; | ||
import { postGithubCode } from '../actions/login'; | ||
import { withRouter } from 'react-router'; | ||
import { Icon, Message } from 'semantic-ui-react'; | ||
|
||
class GitHubAuth extends Component { | ||
constructor(props) { | ||
super(props); | ||
this.state = { | ||
code: '', | ||
error: null, | ||
posted: false, | ||
errorText: '', | ||
}; | ||
this.postCode = this.postCode.bind(this); | ||
this.GithubError = this.GithubError.bind(this); | ||
} | ||
|
||
postCode = (response) => { | ||
console.log(response.code); | ||
this.setState({ | ||
code: response.code, | ||
}); | ||
const data = { | ||
code: this.state.code, | ||
}; | ||
this.props.postGithubCode(data, this.callback); | ||
this.setState({ | ||
code: '', | ||
}); | ||
}; | ||
|
||
callback = () => { | ||
this.setState({ | ||
error: this.props.loginerror ? true : false, | ||
posted: true, | ||
errorText: 'Server Error: try again.', | ||
}); | ||
console.log(this.state.error, this.state.posted); | ||
if (!this.state.error) { | ||
this.props.history.push('/'); | ||
} | ||
setTimeout(() => { | ||
this.setState({ | ||
error: false, | ||
posted: false, | ||
}); | ||
}, 4000); | ||
}; | ||
|
||
icon = () => { | ||
return ( | ||
<React.Fragment> | ||
<Icon name="github" /> Log in with GitHub | ||
</React.Fragment> | ||
); | ||
}; | ||
|
||
GithubError = (response) => { | ||
console.log(response); | ||
this.setState({ | ||
error: true, | ||
posted: true, | ||
errorText: 'GitHub API Error.', | ||
}); | ||
setTimeout(() => { | ||
this.setState({ | ||
error: false, | ||
posted: false, | ||
}); | ||
}, 4000); | ||
}; | ||
|
||
UNSAFE_componentWillMount() { | ||
this.setState({ | ||
code: '', | ||
error: null, | ||
posted: false, | ||
errorText: '', | ||
}); | ||
} | ||
|
||
componentWillUnmount() { | ||
// eslint-disable-next-line | ||
this.setState = (state, callback) => { | ||
return; | ||
}; | ||
} | ||
|
||
render() { | ||
const { | ||
REACT_APP_GITHUB_CLIENT_ID, | ||
REACT_APP_GITHUB_CALLBACK_URL, | ||
} = process.env; | ||
const { error, posted, errorText } = this.state; | ||
const onFailure = (response) => this.GithubError(response); | ||
return ( | ||
<> | ||
{error && posted ? ( | ||
<Message error content={errorText} /> | ||
) : ( | ||
<GitHubLogin | ||
clientId={`${REACT_APP_GITHUB_CLIENT_ID}`} | ||
onSuccess={this.postCode} | ||
onFailure={onFailure} | ||
redirectUri={`${REACT_APP_GITHUB_CALLBACK_URL}`} | ||
className="ui fluid button" | ||
> | ||
{this.icon()} | ||
</GitHubLogin> | ||
)} | ||
</> | ||
); | ||
} | ||
} | ||
|
||
GitHubAuth.propTypes = { | ||
postGithubCode: PropTypes.func.isRequired, | ||
}; | ||
|
||
const mapStateToProps = (state) => ({ | ||
login: state.login.login, | ||
loginerror: state.login.loginerror, | ||
}); | ||
|
||
export default connect(mapStateToProps, { postGithubCode })( | ||
withRouter(GitHubAuth) | ||
); |
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