Skip to content
Merged
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
13 changes: 13 additions & 0 deletions src/pages/Login/Login.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ const dispatchMock = jest.fn();
beforeEach(() => {
jest.spyOn(reactRedux, 'useDispatch').mockImplementation(() => dispatchMock);
jest.spyOn(authActions, 'authFacebook').mockImplementation(jest.fn);
jest.spyOn(authActions, 'authGoogle').mockImplementation(jest.fn);
});

it('should dispatch authFacebook action when the user tries to log in with Facebook', () => {
Expand All @@ -59,3 +60,15 @@ it('should dispatch authFacebook action when the user tries to log in with Faceb

expect(authActions.authFacebook).toHaveBeenCalled();
});

it('should dispatch authGoogle action when the user tries to log in with Google', () => {
const { component } = mountWithProviders(<Login />)({
auth: {
userData: {}
}
});

component.find('#google').simulate('click');

expect(authActions.authGoogle).toHaveBeenCalled();
});
5 changes: 3 additions & 2 deletions src/pages/Login/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ import {
auth,
setPassword,
authCleanUp,
authFacebook
authFacebook,
authGoogle
} from 'state/actions/auth';
import { useChangeHandler, useFormatMessage } from 'hooks';
import { inputValidations } from 'utils';
Expand Down Expand Up @@ -75,7 +76,7 @@ const Login = () => {
};

const onGoogleHandler = () => {
alert('Coming soon');
dispatch(authGoogle());
};

const onMicrosoftHandler = () => {
Expand Down
52 changes: 33 additions & 19 deletions src/state/actions/auth.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,11 +61,11 @@ export const AUTH_CHANGE_PASSWORD_FAIL = createAction(

export const AUTH_UPDATE_USER_DATA = createAction('AUTH_UPDATE_USER_DATA');

export const AUTH_FACEBOOK_INIT = createAction('AUTH_FACEBOOK_INIT');
export const AUTH_PROVIDER_INIT = createAction('AUTH_PROVIDER_INIT');

export const AUTH_FACEBOOK_SUCCESS = createAction('AUTH_FACEBOOK_SUCCESS');
export const AUTH_PROVIDER_SUCCESS = createAction('AUTH_PROVIDER_SUCCESS');

export const AUTH_FACEBOOK_FAIL = createAction('AUTH_FACEBOOK_FAIL');
export const AUTH_PROVIDER_FAIL = createAction('AUTH_PROVIDER_FAIL');

export const logout = () => {
return async dispatch => {
Expand Down Expand Up @@ -241,62 +241,76 @@ export const changeUserPassword = (currentPassword, newPassword) => {
};
};

export const authFacebook = () => {
const authWithProvider = provider => {
return async (dispatch, getState) => {
dispatch(AUTH_FACEBOOK_INIT());
dispatch(AUTH_PROVIDER_INIT());
const { locale } = getState().preferences;

const provider = new firebase.auth.FacebookAuthProvider();

provider.addScope('email');
provider.addScope('user_location');

let result;

try {
result = await firebase.auth().signInWithPopup(provider);
} catch (error) {
const errorMessage = firebaseError(error.code, locale);
return dispatch(AUTH_FACEBOOK_FAIL({ error: errorMessage }));
return dispatch(AUTH_PROVIDER_FAIL({ error: errorMessage }));
}
const { user, additionalUserInfo } = result;

const { uid } = firebase.auth().currentUser;

const createdAt = new Date().toString();

const { location } = additionalUserInfo.profile;

const userData = {
isAdmin: false,
email: user.email,
name: user.displayName,
createdAt,
logoUrl: user.photoURL,
location: additionalUserInfo.profile.location.name
location: location ? location.name : null
};
let userFacebook;

let userValue;
try {
userFacebook = (
userValue = (
await firebase
.database()
.ref(`users/${uid}`)
.once('value')
).val();
} catch (error) {
const errorMessage = firebaseError(error.code, locale);
return dispatch(AUTH_FACEBOOK_FAIL({ error: errorMessage }));
return dispatch(AUTH_PROVIDER_FAIL({ error: errorMessage }));
}

if (!userFacebook) {
if (!userValue) {
try {
await firebase
.database()
.ref(`users/${uid}`)
.set(userData);
} catch (error) {
const errorMessage = firebaseError(error.code, locale);
return dispatch(AUTH_FACEBOOK_FAIL({ error: errorMessage }));
return dispatch(AUTH_PROVIDER_FAIL({ error: errorMessage }));
}
}

return dispatch(AUTH_FACEBOOK_SUCCESS({ id: uid, ...userData }));
return dispatch(
AUTH_PROVIDER_SUCCESS({ id: uid, ...userData, ...userValue })
);
};
};

export const authFacebook = () => {
const provider = new firebase.auth.FacebookAuthProvider();
provider.addScope('email');
provider.addScope('user_location');
return authWithProvider(provider);
};

export const authGoogle = () => {
const provider = new firebase.auth.GoogleAuthProvider();
provider.addScope('https://www.googleapis.com/auth/user.addresses.read');
provider.addScope('https://www.googleapis.com/auth/userinfo.email');
return authWithProvider(provider);
};
3 changes: 3 additions & 0 deletions src/state/actions/users.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,9 @@ export const fetchUsers = () => {
};

const deleteLogo = oldLogo => {
if (!oldLogo.includes('firebasestorage')) {
return null;
}
const logoPath = oldLogo
.split('users%2F')
.pop()
Expand Down
18 changes: 9 additions & 9 deletions src/state/reducers/auth/auth.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@ import {
AUTH_CHANGE_PASSWORD_SUCCESS,
AUTH_CHANGE_PASSWORD_FAIL,
AUTH_UPDATE_USER_DATA,
AUTH_FACEBOOK_FAIL,
AUTH_FACEBOOK_INIT,
AUTH_FACEBOOK_SUCCESS
AUTH_PROVIDER_FAIL,
AUTH_PROVIDER_INIT,
AUTH_PROVIDER_SUCCESS
} from 'state/actions/auth';

import { authReducer } from '.';
Expand Down Expand Up @@ -217,31 +217,31 @@ describe('Auth reducer', () => {
});
});

it('should set loading to true when AUTH_FACEBOOK_INIT action is fired', () => {
reducerTest(initialState, AUTH_FACEBOOK_INIT(), {
it('should set loading to true when AUTH_PROVIDER_INIT action is fired', () => {
reducerTest(initialState, AUTH_PROVIDER_INIT(), {
...initialState,
loading: true
});
});

it('should set the state with the corresponding payload, loading to false and error to null when AUTH_FACEBOOK_SUCCESS actions is fired', () => {
it('should set the state with the corresponding payload, loading to false and error to null when AUTH_PROVIDER_SUCCESS actions is fired', () => {
const payload = {
id: 'some user id',
isAdmin: false
};

reducerTest(initialState, AUTH_FACEBOOK_SUCCESS(payload), {
reducerTest(initialState, AUTH_PROVIDER_SUCCESS(payload), {
...initialState,
userData: {
...payload
}
});
});

it('should set loading to false and error with the corresponding payload when AUTH_FACEBOOK_FAIL action is fired', () => {
it('should set loading to false and error with the corresponding payload when AUTH_PROVIDER_FAIL action is fired', () => {
reducerTest(
{ ...initialState, loading: true },
AUTH_FACEBOOK_FAIL({ error: 'sample error' }),
AUTH_PROVIDER_FAIL({ error: 'sample error' }),
{ ...initialState, error: 'sample error' }
);
});
Expand Down
12 changes: 6 additions & 6 deletions src/state/reducers/auth/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@ import {
AUTH_CHANGE_PASSWORD_SUCCESS,
AUTH_CHANGE_PASSWORD_FAIL,
AUTH_UPDATE_USER_DATA,
AUTH_FACEBOOK_FAIL,
AUTH_FACEBOOK_INIT,
AUTH_FACEBOOK_SUCCESS
AUTH_PROVIDER_FAIL,
AUTH_PROVIDER_INIT,
AUTH_PROVIDER_SUCCESS
} from 'state/actions/auth';

const initialState = {
Expand Down Expand Up @@ -144,11 +144,11 @@ export const authReducer = createReducer(
createdAt: payload.createdAt
}
}),
[AUTH_FACEBOOK_INIT]: state => ({
[AUTH_PROVIDER_INIT]: state => ({
...state,
loading: true
}),
[AUTH_FACEBOOK_SUCCESS]: (state, payload) => ({
[AUTH_PROVIDER_SUCCESS]: (state, payload) => ({
...state,
userData: {
id: payload.id,
Expand All @@ -162,7 +162,7 @@ export const authReducer = createReducer(
error: null,
loading: false
}),
[AUTH_FACEBOOK_FAIL]: (state, payload) => ({
[AUTH_PROVIDER_FAIL]: (state, payload) => ({
...state,
loading: false,
error: payload.error
Expand Down