Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added GitHub Oauth Login and Registration #107

Open
wants to merge 3 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 3 additions & 6 deletions .github/workflows/qa-checks.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,6 @@ jobs:
run: npm install

- name: Linters and Formatters Check
uses: wearerequired/lint-action@v1
with:
github_token: ${{ secrets.GITHUB_TOKEN }}
# Enable linters
eslint: true
prettier: true
run: |
npm run lint
npm run check
16 changes: 12 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,16 +24,24 @@ Documentation for the project is hosted [here](https://osp-web-docs.surge.sh/).

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

1. To start the server:
1. Create a `.env` file in the project root directory and add **Client ID** and **Callback URL** of GitHub App like this:

```
REACT_APP_GITHUB_CLIENT_ID=< GitHub App Client ID >
REACT_APP_GITHUB_CALLBACK_URL=< GitHub App Callback URL >
```

To get **Client ID** and **Callback URL** of GitHub App follow [this docs](https://docs.github.com/en/developers/apps/creating-a-github-app).

2. To start the server:

```
cd open-source-programs-web
npm install
npm start
```

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

## Contributing

Expand Down
9 changes: 6 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,18 @@
"@testing-library/react": "^9.3.2",
"@testing-library/user-event": "^7.1.2",
"aws-sdk": "^2.734.0",
"axios": "^0.19.2",
"axios": "^0.21.1",
"dotenv": "^8.2.0",
"github-login": "^1.0.12",
"moment": "^2.27.0",
"prop-types": "^15.7.2",
"react": "^16.13.1",
"react-dom": "^16.13.1",
"react-github-login": "^1.0.3",
"react-redux": "^7.2.0",
"react-router-dom": "^5.2.0",
"react-router-redux": "^4.0.8",
"react-scripts": "3.4.1",
"react-scripts": "^4.0.3",
"redux": "^4.0.5",
"redux-api-middleware": "^3.2.1",
"redux-persist": "^6.0.0",
Expand All @@ -33,7 +35,8 @@
"eject": "react-scripts eject",
"lint": "eslint .",
"lint:fix": "eslint . --fix",
"format": "prettier --write \"**/*.+(js|jsx|json|css|md)\""
"format": "prettier --write \"**/*.+(js|jsx|json|css|md)\"",
"check": "prettier -c \"**/*.+(js|jsx|json|css|md)\""
},
"browserslist": {
"production": [
Expand Down
9 changes: 0 additions & 9 deletions src/App.test.js

This file was deleted.

26 changes: 25 additions & 1 deletion src/actions/login.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import axios from 'axios';
import { urlLogin, urlRegister } from '../urls';
import { urlLogin, urlRegister, urlGithubLogin } from '../urls';
import { LOGIN, REGISTER, LOGIN_ERRORS, REGISTER_ERRORS } from './types';

export const postLogin = (data, callback) => async (dispatch) => {
Expand Down Expand Up @@ -48,3 +48,27 @@ export const postRegister = (data, callback) => async (dispatch) => {
callback();
}
};

export const postGithubCode = (data, callback) => async (dispatch) => {
try {
const config = {
headers: {
'content-type': 'application/json',
},
};
const res = await axios.post(urlGithubLogin(), data, config);
dispatch({
type: LOGIN,
payload: res.data,
});

localStorage.setItem('token', res.data.access_token);
callback();
} catch (err) {
dispatch({
type: LOGIN_ERRORS,
payload: err.response,
});
callback();
}
};
128 changes: 128 additions & 0 deletions src/components/GitHubAuth.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
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 { 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) => {
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.',
});
if (!this.state.error) {
this.props.history.push('/');
}
setTimeout(() => {
this.setState({
error: false,
posted: false,
});
}, 4000);
};

githubIcon = () => {
return (
<React.Fragment>
<div data-testid="GitHubLoginButton">
<Icon name="github" /> Log in with GitHub
</div>
</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);
};

componentDidMount() {
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.githubIcon()}
</GitHubLogin>
)}
</>
);
}
}

GitHubAuth.propTypes = {
postGithubCode: PropTypes.func.isRequired,
};

const mapStateToProps = (state) => ({
login: state.login.login,
loginerror: state.login.loginerror,
});

export default connect(mapStateToProps, { postGithubCode })(GitHubAuth);
5 changes: 5 additions & 0 deletions src/components/Login.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,13 @@ import {
Icon,
Message,
Button,
Segment,
} from 'semantic-ui-react';
import PropTypes from 'prop-types';
import './../styles/Login.css';
import orgLogo from '../assets/org-logo.jpg';
import { register } from '../urls';
import GitHubAuth from './GitHubAuth';

class Login extends Component {
constructor(props) {
Expand Down Expand Up @@ -168,6 +170,9 @@ class Login extends Component {
<Message error content={this.props.loginerror.detail} />
) : null}
<Divider />
<Segment>
<GitHubAuth {...this.props} />
</Segment>
<span>
Don't have an account?{' '}
<Link to={register()}>Register here.</Link>
Expand Down
25 changes: 25 additions & 0 deletions src/tests/GitHubAuth.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import React from 'react';
import { createStore } from 'redux';
import { Provider } from 'react-redux';
import { render, cleanup } from '@testing-library/react';
import '@testing-library/jest-dom';
import GitHubAuth from '../components/GitHubAuth';
import loginReducer from '../reducers/login';

afterEach(cleanup);

function renderWithRedux(
component,
{ initialState, store = createStore(loginReducer, initialState) } = {}
) {
return {
...render(<Provider store={store}>{component}</Provider>),
};
}

it('Renders GitHub Login Button', () => {
const { getByTestId } = renderWithRedux(<GitHubAuth />);
expect(getByTestId('GitHubLoginButton')).toHaveTextContent(
'Log in with GitHub'
);
});
4 changes: 4 additions & 0 deletions src/urls.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,10 @@ export function urlRegister() {
return `${urlBaseBackend()}/token_auth/register/`;
}

export function urlGithubLogin() {
return `${urlBaseBackend()}/token_auth/github/`;
}

export function urlInfo() {
return `${urlBaseBackend()}/info/`;
}
Expand Down