Skip to content

Added Google Oauth Signup #112

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

Open
wants to merge 6 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
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
2 changes: 2 additions & 0 deletions .env.sample
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
REACT_APP_GOOGLE_CLIENT_ID=<Google App Client ID>
REACT_APP_GOOGLE_CALLBACK_URL=<Google Callback URL>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add a new line at the end.

15 changes: 11 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,23 @@ Open Source Programs (OSP) is an application that simplifies the processing and
To setup the project locally, go through [this wiki page](https://github.com/anitab-org/open-source-programs-web/wiki/Fork,-Clone,-Remote-and-Pull-Request).
**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 Google like this:

```
REACT_APP_GOOGLE_CLIENT_ID=<Google App Client ID>
REACT_APP_GOOGLE_CALLBACK_URL=<Google Callback URL>
```
To get **Client ID** and **Callback URL** of Google OAuth App follow [this docs](https://developers.google.com/adwords/api/docs/guides/authentication#create_a_client_id_and_client_secret).

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
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
"prop-types": "^15.7.2",
"react": "^16.13.1",
"react-dom": "^16.13.1",
"react-google-login": "^5.2.2",
"react-redux": "^7.2.0",
"react-router-dom": "^5.2.0",
"react-router-redux": "^4.0.8",
Expand Down
37 changes: 36 additions & 1 deletion src/actions/login.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import axios from 'axios'
import {
urlLogin,
urlRegister
urlRegister,
urlGoogleLogin,
} from '../urls'
import {
LOGIN,
Expand All @@ -10,6 +11,40 @@ import {
REGISTER_ERRORS
} from './types'



export const GoogleOauthLogin = (data,callback) => async dispatch => {
try {
const config = {
headers: {
'content-type': 'application/json',
}
}
const obj = {
access_token:data.tokenObj.access_token,
id_token:data.tokenObj.id_token
}
console.log(obj);
const res = await axios.post(urlGoogleLogin(), obj , config);
console.log(res);
dispatch({
type: LOGIN,
payload: res.data
});

// set token value
localStorage.setItem('token', res.data.access_token)
callback()
}
catch (err) {
dispatch({
type: LOGIN_ERRORS,
payload: err.response
});
callback()
}
};

export const postLogin = (data, callback) => async dispatch => {
try {
const config = {
Expand Down
123 changes: 123 additions & 0 deletions src/components/GoogleSocialAuth.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
import React, { Component } from 'react';
import GoogleLogin from 'react-google-login';
import { GoogleOauthLogin } from '../actions/login';
import { connect } from 'react-redux';
import PropTypes from 'prop-types';
import { withRouter } from 'react-router';
import { Message } from 'semantic-ui-react'
import "../styles/google-auth-styles.css";

class GoogleSocialAuth extends Component {
constructor(props){
super(props)
this.state={
statusCode:'',
error:null,
posted:false,
errorText:''
}
}

responseCode = (response) => {
this.setState({
statusCode: response.code
})
const data = {
statusCode: this.state.code,
...response
}
this.props.GoogleOauthLogin(data, this.callback)
this.setState({
statusCode: ''
})
}

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)
}

GoogleError = (response) => {
console.log(response);
this.setState({
error: true,
posted: true,
errorText: "Google API Error."
})
setTimeout(() => {
this.setState({
error: false,
posted: false
})
}, 4000)
}


componentWillMount() {
this.setState({
statusCode: '',
error: null,
posted: false,
errorText: ''
})
}

componentWillUnmount() {
this.setState = (state, callback) => {
return;
};
}
render() {
const { REACT_APP_GOOGLE_CLIENT_ID, REACT_APP_GOOGLE_CALLBACK_URL } = process.env;
const { error, posted, errorText } = this.state;
const onFailure = response => this.GoogleError(response)

return (
<>
{
error && posted ?
<Message
error
content={errorText}
/> :
<div className="google-login">
<GoogleLogin
clientId={`${REACT_APP_GOOGLE_CLIENT_ID}`}
onSuccess={this.responseCode}
onFailure={onFailure}
buttonText="SignUp with Google"
redirectUri={`${REACT_APP_GOOGLE_CALLBACK_URL}`}
className="google-cust-button"
/>
</div>
}
</>
);
}
}

GoogleSocialAuth.propTypes = {
GoogleOauthLogin: PropTypes.func.isRequired,
}
const mapStateToProps = state => ({
login: state.login.login,
loginerror: state.login.loginerror
})

export default connect(
mapStateToProps,
{ GoogleOauthLogin, }
)(withRouter(GoogleSocialAuth))
7 changes: 5 additions & 2 deletions src/components/Login.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@ import React, { Component } from 'react'
import { connect } from 'react-redux'
import { Link } from 'react-router-dom'
import { postLogin } from '../actions/login'
import { Form, Grid, Image, Divider, Icon, Message, Button } from 'semantic-ui-react'
import { Form, Grid, Image, Divider, Icon, Message, Button ,Segment } from 'semantic-ui-react'
import PropTypes from 'prop-types'
import login from './../styles/Login.css'
import orgLogo from '../assets/org-logo.jpg'
import { register } from '../urls'

import GoogleAuth from './GoogleSocialAuth'
class Login extends Component {
constructor(props){
super(props)
Expand Down Expand Up @@ -162,6 +162,9 @@ class Login extends Component {
: null
}
<Divider />
<Segment>
<GoogleAuth />
</Segment>
<span>Don't have an account? <Link to={register()}>Register here.</Link></span>
</div>
</Grid.Column>
Expand Down
4 changes: 4 additions & 0 deletions src/styles/google-auth-styles.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
.google-cust-button{
width: 100%;
justify-content: center;
}
5 changes: 5 additions & 0 deletions src/urls.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,11 @@ export function urlRegister() {
return `${urlBaseBackend()}/token_auth/register/`
}


export function urlGoogleLogin() {
return `${urlBaseBackend()}/token_auth/google/`
}

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