-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(social-auth): social-authentication using google and facebook
- install react-facebook-login and react-google-login modules - create a social authentication component - create actions for the social authentication component - create a reducer for the social authentication component - add the route to the component - add backend api urls to the config.json file - add tests for socialauth functionality - put test folder into src - refactor jest.config.js - install moxios and react-mock for testing [Finishes #164798254]
- Loading branch information
Kisekka David
authored and
Kisekka David
committed
May 14, 2019
1 parent
29749e4
commit 95cab99
Showing
23 changed files
with
1,286 additions
and
50 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
import endpoints from "../urls"; | ||
import { | ||
GOOGLE_FAILURE, | ||
GOOGLE_LOGIN, | ||
FACEBOOK_FAILURE, | ||
FACEBOOK_LOGIN | ||
} from "./types"; | ||
import axios from "axios"; | ||
import "react-toastify/dist/ReactToastify.css"; | ||
import { toast } from "react-toastify"; | ||
|
||
export const facebooklogin = token => dispatch => { | ||
const body = { | ||
user_token: { | ||
auth_token: token | ||
} | ||
}; | ||
|
||
const headers = { | ||
"Content-Type": "application/json" | ||
}; | ||
return axios | ||
.post(endpoints.facebookLogin, body, { headers: headers }) | ||
.then(data => { | ||
if (data.data.errors) { | ||
dispatch({ | ||
type: FACEBOOK_FAILURE, | ||
payload: data.data | ||
}); | ||
} else { | ||
if (data.data["auth_token"].username) { | ||
toast.success( | ||
`Welcome ${data.data["auth_token"].username}. Login Successful`, | ||
{ | ||
position: toast.POSITION.TOP_RIGHT, | ||
autoClose: 5000, | ||
hideProgressBar: false | ||
} | ||
); | ||
dispatch({ | ||
type: FACEBOOK_LOGIN, | ||
payload: data.data["auth_token"] | ||
}); | ||
} else { | ||
toast.success(`${data.data["auth_token"]}`, { | ||
position: toast.POSITION.TOP_RIGHT, | ||
autoClose: 5000, | ||
hideProgressBar: false | ||
}); | ||
} | ||
} | ||
}); | ||
}; | ||
|
||
export const googlelogin = token => dispatch => { | ||
const body = { | ||
user_token: { | ||
auth_token: token | ||
} | ||
}; | ||
|
||
const headers = { | ||
"Content-Type": "application/json" | ||
}; | ||
return axios | ||
.post(endpoints.googleLogin, body, { headers: headers }) | ||
.then(data => { | ||
if (data.data.errors) { | ||
dispatch({ | ||
type: GOOGLE_FAILURE, | ||
payload: data.data | ||
}); | ||
} else { | ||
if (data.data["auth_token"].username) { | ||
toast.success( | ||
`Welcome ${data.data["auth_token"].username}. Login Successful`, | ||
{ | ||
position: toast.POSITION.TOP_RIGHT, | ||
autoClose: 5000, | ||
hideProgressBar: false | ||
} | ||
); | ||
dispatch({ | ||
type: GOOGLE_LOGIN, | ||
payload: data.data["auth_token"] | ||
}); | ||
} else { | ||
toast.success(`${data.data["auth_token"]}`, { | ||
position: toast.POSITION.TOP_RIGHT, | ||
autoClose: 5000, | ||
hideProgressBar: false | ||
}); | ||
} | ||
} | ||
}); | ||
}; |
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,37 @@ | ||
import React from "react"; | ||
import FacebookLogin from "react-facebook-login"; | ||
import GoogleLogin from "react-google-login"; | ||
import config from "../../src/config.json"; | ||
import color from "@material-ui/core/colors/grey"; | ||
|
||
export const SocialAuth = props => ( | ||
<div> | ||
<div className="face-book"> | ||
<FacebookLogin | ||
appId={config.appId} | ||
fields="name,email,picture" | ||
callback={props.facebooksuccess} | ||
icon={ | ||
<img | ||
src="https://img.icons8.com/color/96/000000/facebook.png" | ||
width="=40px" | ||
height="40px" | ||
alt="facebook" | ||
/> | ||
} | ||
textButton="SIGN IN WITH FACEBOOK" | ||
/> | ||
</div> | ||
<br /> | ||
<div className="google"> | ||
<GoogleLogin | ||
clientId={config.clientId} | ||
buttonText="SIGN IN WITH GOOGLE" | ||
onSuccess={props.googlesuccess} | ||
onFailure={props.googlefailure} | ||
/> | ||
</div> | ||
</div> | ||
); | ||
|
||
export default SocialAuth; |
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,4 @@ | ||
{ | ||
"appId": 281662639446583, | ||
"clientId": "140929687593-ul95834mce97cv333q6ci5i78rnmhu87.apps.googleusercontent.com" | ||
} |
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,60 @@ | ||
import React, { Component } from "react"; | ||
import { connect } from "react-redux"; | ||
import { facebooklogin, googlelogin } from "../actions/SocialAuthActions"; | ||
import { SocialAuth } from "../components/SocialAuth"; | ||
import { toast } from "react-toastify"; | ||
import "react-toastify/dist/ReactToastify.css"; | ||
import { withRouter } from "react-router-dom"; | ||
|
||
export class SocialAuthContainer extends Component { | ||
constructor(props) { | ||
super(props); | ||
} | ||
componentWillReceiveProps(nextProps) { | ||
if (nextProps.socialAuthState.isAuthenticated) { | ||
localStorage.setItem("token", nextProps.socialAuthState.payload); | ||
this.props.history.push("/"); | ||
} | ||
} | ||
|
||
handlefacebooksuccess = response => { | ||
const { facebooklogin } = this.props; | ||
if (response.accessToken) { | ||
facebooklogin(response.accessToken); | ||
} | ||
}; | ||
handlegooglesuccess = response => { | ||
const { googlelogin } = this.props; | ||
if (response.tokenId) { | ||
googlelogin(response.tokenId); | ||
} | ||
}; | ||
handlegooglefailure = response => { | ||
const { googlelogin } = this.props; | ||
googlelogin("invalid request"); | ||
}; | ||
|
||
render() { | ||
return ( | ||
<div> | ||
<SocialAuth | ||
id="socialAuth" | ||
facebooksuccess={this.handlefacebooksuccess} | ||
googlesuccess={this.handlegooglesuccess} | ||
googlefailure={this.handlegooglefailure} | ||
/> | ||
</div> | ||
); | ||
} | ||
} | ||
|
||
export const mapStateToProps = state => ({ | ||
socialAuthState: state.socialAuthReducer | ||
}); | ||
|
||
export default withRouter( | ||
connect( | ||
mapStateToProps, | ||
{ facebooklogin, googlelogin } | ||
)(SocialAuthContainer) | ||
); |
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,49 @@ | ||
import { | ||
GOOGLE_FAILURE, | ||
GOOGLE_LOGIN, | ||
FACEBOOK_FAILURE, | ||
FACEBOOK_LOGIN | ||
} from "../actions/types"; | ||
|
||
const initialState = { | ||
isAuthenticated: false, | ||
facebook_login: false, | ||
google_login: false, | ||
payload: "", | ||
token: "" | ||
}; | ||
|
||
const socialAuthReducer = (state = initialState, action) => { | ||
switch (action.type) { | ||
case FACEBOOK_LOGIN: | ||
return { | ||
...state, | ||
isAuthenticated: true, | ||
facebook_login: true, | ||
token: action.token, | ||
payload: action.payload | ||
}; | ||
case FACEBOOK_FAILURE: | ||
return { | ||
...state, | ||
payload: action.payload | ||
}; | ||
case GOOGLE_LOGIN: | ||
return { | ||
...state, | ||
isAuthenticated: true, | ||
google_login: true, | ||
token: action.token, | ||
payload: action.payload | ||
}; | ||
case GOOGLE_FAILURE: | ||
return { | ||
...state, | ||
payload: action.payload | ||
}; | ||
default: | ||
return state; | ||
} | ||
}; | ||
|
||
export default socialAuthReducer; |
Oops, something went wrong.