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

Social login configuration #407

Open
wants to merge 3 commits into
base: main
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
4 changes: 3 additions & 1 deletion src/App.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import SignupScreen from './screens/signUp/SignupScreen'
import CongratulationScreen from './screens/congratulation/CongratulationScreen'
import Messenger from './screens/messenger/Messenger'
import Library from './screens/library/Library'
import LoginRedirect from './components/loginRedirect/LoginRedirect'
import './App.css'
import DashboardComponent from './screens/dashboard/MainDashboard'
import Achievements from './screens/dashboard/Achievements'
Expand Down Expand Up @@ -58,11 +59,12 @@ function App () {
<Router>
<ScrollToTop>
<Switch>
<Route component={LoginRedirect} path='/redirect' exact />
<Route component={LoginScreen} path='/login' exact />
<Route component={SignupScreen} path='/register' exact />
<Route component={ForgotPassword} path='/forgot-password' />
<Route component={() => <Redirect to='/login' />} path='/' exact />
<PrivateRoute component={UserVerification} path='/verification' exact />
<PrivateRoute component={() => <Redirect to='/login' />} path='/' exact />
<PrivateRoute component={LogoutUser} path='/logout' exact />
<PrivateRoute component={CongratulationScreen} path='/edit-information' exact />
<PrivateRoute component={CalendarScreen} exact path='/calendar/my-events' />
Expand Down
76 changes: 67 additions & 9 deletions src/actions/userAction.js
Original file line number Diff line number Diff line change
Expand Up @@ -91,8 +91,8 @@ if (process.env.REACT_APP_AUTH_METHOD === 'cognito') {
oauth: {
domain: process.env.REACT_APP_COGNITO_DOMAIN_NAME, // domain_name
scope: ['phone', 'email', 'profile', 'openid', 'aws.cognito.signin.user.admin'],
redirectSignIn: process.env.FRONTEND_URL,
redirectSignOut: process.env.FRONTEND_URL,
redirectSignIn: process.env.REACT_APP_FRONTEND_URL,
redirectSignOut: process.env.REACT_APP_FRONTEND_URL,
responseType: 'token' // or 'token', note that REFRESH token will only be generated when the responseType is code
}
}
Expand Down Expand Up @@ -137,6 +137,64 @@ export const register = (name, password) => async (dispatch) => {
}
}

export const socialSignIn = (provider) => (dispatch) => {
try {
Auth.federatedSignIn({ provider })
} catch (error) {
dispatch({
type: USER_LOGIN_FAIL,
payload: error.response && error.response.data.error ? error.response.data.error : error.message
})
}
}

export const socialSignInRedirect = (provider) => async (dispatch) => {
let data = {}
try {
dispatch({ type: USER_LOGIN_REQUEST })
const response = await Auth.currentAuthenticatedUser()
if (response) {
data = {
token: response?.signInUserSession?.idToken?.jwtToken || '',
id: response?.attributes?.sub || ''
}
window.localStorage.setItem('userInfo', JSON.stringify(data))
await postApi(dispatch, `${process.env.REACT_APP_API_BASE_URL}/api/users`, { id: data.id }, {
headers: {
Authorization: 'Bearer ' + data.token
}
})

await axios.put(`${process.env.REACT_APP_API_BASE_URL}/api/users/profile`, {
firstName: response.attributes.given_name,
lastName: response.attributes.family_name,
birthday: response.attributes.birthdate,
phone: response.attributes.phone_number,
email: response.attributes.email
},
{
headers: {
Authorization: 'Bearer ' + data.token
}
})

window.localStorage.setItem('userInfo', JSON.stringify(data))
dispatch({ type: USER_LOGIN_SUCCESS, payload: data })
await routingCommunityNews(dispatch, true)
} else {
window.location.href = '/login'
console.log('Not logged in')
}
} catch (error) {
dispatch({
type: USER_LOGIN_FAIL,
payload: error.response && error.response.data.error
? error.response.data.error
: error.message
})
}
}

export const login = (name, password) => async (dispatch) => {
let data = {}
try {
Expand Down Expand Up @@ -213,7 +271,7 @@ export const checkAndUpdateToken = () => async (dispatch) => {
}
}).catch((data) => {
return tokenFailure(dispatch, 'Unauthorized')
/*const message = data.response && data.response.data.name ? data.response.data.name : data.message
/* const message = data.response && data.response.data.name ? data.response.data.name : data.message
if (message === 'TokenExpired') {
if (process.env.REACT_APP_AUTH_METHOD === 'cognito') {
Auth.currentSession().then((res) => {
Expand Down Expand Up @@ -242,7 +300,7 @@ export const checkAndUpdateToken = () => async (dispatch) => {
}
} else {
return tokenFailure(dispatch, 'Unauthorized')
}*/
} */
})
}

Expand All @@ -256,6 +314,8 @@ const tokenFailure = (dispatch, message) => {

export const getMyDetails = () => async (dispatch) => {
try {
const response = await Auth.currentAuthenticatedUser()
console.log(response)
dispatch({ type: USER_DETAILS_REQUEST })
const { data } = await getApi(dispatch, `${process.env.REACT_APP_API_BASE_URL}/api/users/profile`)
const userdata = {
Expand Down Expand Up @@ -357,11 +417,9 @@ export const searchUsers = (search) => async (dispatch) => {
}

export const logout = () => (dispatch) => {
Auth.signOut().then(() => {
window.localStorage.clear()
dispatch({ type: USER_LOGOUT })
document.location.href = '/login'
}).catch(err => console.log(err))
window.localStorage.clear()
dispatch({ type: USER_LOGOUT })
document.location.href = '/login'
}

export const confirmPin = (username, code) => async (dispatch) => {
Expand Down
19 changes: 19 additions & 0 deletions src/components/loginRedirect/LoginRedirect.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import React, { useEffect } from 'react'
import { useDispatch } from 'react-redux'
import { socialSignInRedirect } from '../../actions/userAction'

const LoginRedirect = () => {
const dispatch = useDispatch()

useEffect(() => {
dispatch(socialSignInRedirect())
}, [])

return (
<div>
Redirecting...
</div>
)
}

export default LoginRedirect
17 changes: 12 additions & 5 deletions src/components/signInSignUp/SignIn.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@ import { Link, useHistory } from 'react-router-dom'
import { useDispatch, useSelector } from 'react-redux'
import { useForm } from 'react-hook-form'

import { login } from '../../actions/userAction'
import { login, socialSignIn } from '../../actions/userAction'
import { USER_LOGIN_SUCCESS } from '../../constants/userConstants'
import { SignInSignUpData } from './SignInSignUpData'
import { Auth } from 'aws-amplify'

import Button from '../button/Button'
import Checkbox from '../checkbox/Checkbox'
Expand Down Expand Up @@ -37,6 +38,12 @@ const SignIn = () => {
setShowPassword(!showPassword)
}

useEffect(async () => {
const response = await Auth.currentAuthenticatedUser()
console.log(response)
if (response) history.push('/redirect')
}, [])

useEffect(() => {
/* Hub.listen("auth", ({ payload: { event, data } }) => {
console.log(event);
Expand Down Expand Up @@ -98,20 +105,20 @@ const SignIn = () => {

const loginWithFacebook = (e) => {
e.preventDefault()
// Auth.federatedSignIn({ provider: "Facebook" });
dispatch(socialSignIn('Facebook'))
}

const loginWithGoogle = (e) => {
e.preventDefault()
// Auth.federatedSignIn({ provider: "Google" });
dispatch(socialSignIn('Google'))
}

const onSubmit = ({ username, password }) => {
dispatch(login(username, password))
}

return (
<form className='sign' onSubmit={handleSubmit(onSubmit)}>
<form className='sign'>
<h1 className='welcome'>Sign In</h1>
<div className='container'>
{error && <div className='error'>{error}</div>}
Expand Down Expand Up @@ -157,7 +164,7 @@ const SignIn = () => {
</div>

<div className='btnWrapper'>
<Button name='Sign In' />
<Button name='Sign In' onClick={handleSubmit(onSubmit)} />
<Link to='/forgot-password' className='fPassword green16px'>
Forgot Password?
</Link>
Expand Down