Skip to content

Commit 06b9f84

Browse files
committed
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
1 parent 8e97e0f commit 06b9f84

File tree

6 files changed

+259
-86
lines changed

6 files changed

+259
-86
lines changed

README.md

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,16 +21,23 @@ Documentation for the project is hosted [here](https://osp-web-docs.surge.sh/).
2121

2222
**Note:** Before setting up the frontend make sure to have Setup the [Backend Repo](https://github.com/anitab-org/open-source-programs-backend).
2323

24-
1. To start the server:
24+
1. Create a `.env` file in the project root directory and add **Client ID** and **Callback URL** of GitHub App like this:
25+
26+
```
27+
REACT_APP_GITHUB_CLIENT_ID=< GitHub App Client ID >
28+
REACT_APP_GITHUB_CALLBACK_URL=< GitHub App Callback URL >
29+
```
30+
To get **Client ID** and **Callback URL** of GitHub App follow [this docs](https://docs.github.com/en/developers/apps/creating-a-github-app).
31+
32+
2. To start the server:
2533

2634
```
27-
cd open-source-programs-web
2835
npm install
2936
npm start
3037
```
3138

32-
2. Navigate to `http://localhost:3000/` in your browser.
33-
3. You can terminate the process by `Ctrl+C` in your terminal.
39+
3. Navigate to `http://localhost:3000/` in your browser.
40+
4. You can terminate the process by `Ctrl+C` in your terminal.
3441

3542
## Contributing
3643

package.json

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,18 @@
77
"@testing-library/react": "^9.3.2",
88
"@testing-library/user-event": "^7.1.2",
99
"aws-sdk": "^2.734.0",
10-
"axios": "^0.19.2",
10+
"axios": "^0.21.1",
1111
"dotenv": "^8.2.0",
12+
"github-login": "^1.0.12",
1213
"moment": "^2.27.0",
1314
"prop-types": "^15.7.2",
1415
"react": "^16.13.1",
1516
"react-dom": "^16.13.1",
17+
"react-github-login": "^1.0.3",
1618
"react-redux": "^7.2.0",
1719
"react-router-dom": "^5.2.0",
1820
"react-router-redux": "^4.0.8",
19-
"react-scripts": "3.4.1",
21+
"react-scripts": "^4.0.3",
2022
"redux": "^4.0.5",
2123
"redux-api-middleware": "^3.2.1",
2224
"redux-persist": "^6.0.0",

src/actions/login.js

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
import axios from 'axios'
22
import {
33
urlLogin,
4-
urlRegister
4+
urlRegister,
5+
urlGithubLogin
56
} from '../urls'
67
import {
78
LOGIN,
@@ -58,3 +59,28 @@ export const postRegister = (data, callback) => async dispatch => {
5859
callback()
5960
}
6061
};
62+
63+
export const postGithubCode = (data, callback) => async dispatch => {
64+
try {
65+
const config = {
66+
headers: {
67+
'content-type': 'application/json',
68+
}
69+
}
70+
const res = await axios.post(urlGithubLogin(), data, config);
71+
dispatch({
72+
type: LOGIN,
73+
payload: res.data
74+
});
75+
76+
localStorage.setItem('token', res.data.access_token)
77+
callback();
78+
}
79+
catch (err) {
80+
dispatch({
81+
type: LOGIN_ERRORS,
82+
payload: err.response
83+
});
84+
callback()
85+
}
86+
}

src/components/GitHubAuth.js

Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
1+
import React, { Component } from 'react'
2+
import { connect } from 'react-redux'
3+
import PropTypes from 'prop-types'
4+
import GitHubLogin from 'react-github-login';
5+
import { postGithubCode } from '../actions/login'
6+
import { withRouter } from 'react-router';
7+
import { Icon, Message } from 'semantic-ui-react'
8+
9+
10+
class GitHubAuth extends Component {
11+
12+
constructor(props) {
13+
super(props)
14+
this.state = {
15+
code: '',
16+
error: null,
17+
posted: false,
18+
errorText: ''
19+
}
20+
this.postCode = this.postCode.bind(this);
21+
this.GithubError = this.GithubError.bind(this);
22+
}
23+
24+
postCode = (response) => {
25+
console.log(response.code)
26+
this.setState({
27+
code: response.code
28+
})
29+
const data = {
30+
code: this.state.code
31+
}
32+
this.props.postGithubCode(data, this.callback)
33+
this.setState({
34+
code: ''
35+
})
36+
}
37+
38+
callback = () => {
39+
this.setState({
40+
error: this.props.loginerror ? true : false,
41+
posted: true,
42+
errorText: 'Server Error: try again.'
43+
})
44+
console.log(this.state.error, this.state.posted);
45+
if (!this.state.error) {
46+
this.props.history.push('/')
47+
}
48+
setTimeout(() => {
49+
this.setState({
50+
error: false,
51+
posted: false,
52+
})
53+
}, 4000)
54+
}
55+
56+
icon = () => {
57+
return (
58+
<React.Fragment>
59+
<Icon name='github' /> Log in with GitHub
60+
</React.Fragment>
61+
)
62+
}
63+
64+
GithubError = (response) => {
65+
console.log(response);
66+
this.setState({
67+
error: true,
68+
posted: true,
69+
errorText: "GitHub API Error."
70+
})
71+
setTimeout(() => {
72+
this.setState({
73+
error: false,
74+
posted: false
75+
})
76+
}, 4000)
77+
}
78+
79+
componentWillMount() {
80+
this.setState({
81+
code: '',
82+
error: null,
83+
posted: false,
84+
errorText: ''
85+
})
86+
}
87+
88+
componentWillUnmount() {
89+
this.setState = (state, callback) => {
90+
return;
91+
};
92+
}
93+
94+
render() {
95+
const { REACT_APP_GITHUB_CLIENT_ID, REACT_APP_GITHUB_CALLBACK_URL } = process.env;
96+
const { error, posted, errorText } = this.state;
97+
const onFailure = response => this.GithubError(response)
98+
return (
99+
<>
100+
{
101+
error && posted ?
102+
<Message
103+
error
104+
content={errorText}
105+
/> :
106+
<GitHubLogin clientId={`${REACT_APP_GITHUB_CLIENT_ID}`}
107+
onSuccess={this.postCode}
108+
onFailure={onFailure}
109+
redirectUri={`${REACT_APP_GITHUB_CALLBACK_URL}`}
110+
children={this.icon()}
111+
className="ui fluid button"
112+
/>
113+
}
114+
</>
115+
116+
)
117+
}
118+
}
119+
120+
GitHubAuth.propTypes = {
121+
postGithubCode: PropTypes.func.isRequired,
122+
}
123+
124+
const mapStateToProps = state => ({
125+
login: state.login.login,
126+
loginerror: state.login.loginerror
127+
})
128+
129+
export default connect(
130+
mapStateToProps,
131+
{ postGithubCode, }
132+
)(withRouter(GitHubAuth))

0 commit comments

Comments
 (0)