Skip to content

Commit c4feb30

Browse files
authored
Merge pull request #45 from CreateThrive/feature/google-auth
Feature: Google Authentication
2 parents e619c8d + 6433f0f commit c4feb30

6 files changed

Lines changed: 67 additions & 36 deletions

File tree

src/pages/Login/Login.test.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ const dispatchMock = jest.fn();
4646
beforeEach(() => {
4747
jest.spyOn(reactRedux, 'useDispatch').mockImplementation(() => dispatchMock);
4848
jest.spyOn(authActions, 'authFacebook').mockImplementation(jest.fn);
49+
jest.spyOn(authActions, 'authGoogle').mockImplementation(jest.fn);
4950
});
5051

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

6061
expect(authActions.authFacebook).toHaveBeenCalled();
6162
});
63+
64+
it('should dispatch authGoogle action when the user tries to log in with Google', () => {
65+
const { component } = mountWithProviders(<Login />)({
66+
auth: {
67+
userData: {}
68+
}
69+
});
70+
71+
component.find('#google').simulate('click');
72+
73+
expect(authActions.authGoogle).toHaveBeenCalled();
74+
});

src/pages/Login/index.jsx

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@ import {
1111
auth,
1212
setPassword,
1313
authCleanUp,
14-
authFacebook
14+
authFacebook,
15+
authGoogle
1516
} from 'state/actions/auth';
1617
import { useChangeHandler, useFormatMessage } from 'hooks';
1718
import { inputValidations } from 'utils';
@@ -75,7 +76,7 @@ const Login = () => {
7576
};
7677

7778
const onGoogleHandler = () => {
78-
alert('Coming soon');
79+
dispatch(authGoogle());
7980
};
8081

8182
const onMicrosoftHandler = () => {

src/state/actions/auth.js

Lines changed: 33 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -61,11 +61,11 @@ export const AUTH_CHANGE_PASSWORD_FAIL = createAction(
6161

6262
export const AUTH_UPDATE_USER_DATA = createAction('AUTH_UPDATE_USER_DATA');
6363

64-
export const AUTH_FACEBOOK_INIT = createAction('AUTH_FACEBOOK_INIT');
64+
export const AUTH_PROVIDER_INIT = createAction('AUTH_PROVIDER_INIT');
6565

66-
export const AUTH_FACEBOOK_SUCCESS = createAction('AUTH_FACEBOOK_SUCCESS');
66+
export const AUTH_PROVIDER_SUCCESS = createAction('AUTH_PROVIDER_SUCCESS');
6767

68-
export const AUTH_FACEBOOK_FAIL = createAction('AUTH_FACEBOOK_FAIL');
68+
export const AUTH_PROVIDER_FAIL = createAction('AUTH_PROVIDER_FAIL');
6969

7070
export const logout = () => {
7171
return async dispatch => {
@@ -241,62 +241,76 @@ export const changeUserPassword = (currentPassword, newPassword) => {
241241
};
242242
};
243243

244-
export const authFacebook = () => {
244+
const authWithProvider = provider => {
245245
return async (dispatch, getState) => {
246-
dispatch(AUTH_FACEBOOK_INIT());
246+
dispatch(AUTH_PROVIDER_INIT());
247247
const { locale } = getState().preferences;
248-
249-
const provider = new firebase.auth.FacebookAuthProvider();
250-
251-
provider.addScope('email');
252-
provider.addScope('user_location');
253-
254248
let result;
255249

256250
try {
257251
result = await firebase.auth().signInWithPopup(provider);
258252
} catch (error) {
259253
const errorMessage = firebaseError(error.code, locale);
260-
return dispatch(AUTH_FACEBOOK_FAIL({ error: errorMessage }));
254+
return dispatch(AUTH_PROVIDER_FAIL({ error: errorMessage }));
261255
}
262256
const { user, additionalUserInfo } = result;
263257

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

266260
const createdAt = new Date().toString();
261+
262+
const { location } = additionalUserInfo.profile;
263+
267264
const userData = {
268265
isAdmin: false,
269266
email: user.email,
270267
name: user.displayName,
271268
createdAt,
272269
logoUrl: user.photoURL,
273-
location: additionalUserInfo.profile.location.name
270+
location: location ? location.name : null
274271
};
275-
let userFacebook;
272+
273+
let userValue;
276274
try {
277-
userFacebook = (
275+
userValue = (
278276
await firebase
279277
.database()
280278
.ref(`users/${uid}`)
281279
.once('value')
282280
).val();
283281
} catch (error) {
284282
const errorMessage = firebaseError(error.code, locale);
285-
return dispatch(AUTH_FACEBOOK_FAIL({ error: errorMessage }));
283+
return dispatch(AUTH_PROVIDER_FAIL({ error: errorMessage }));
286284
}
287285

288-
if (!userFacebook) {
286+
if (!userValue) {
289287
try {
290288
await firebase
291289
.database()
292290
.ref(`users/${uid}`)
293291
.set(userData);
294292
} catch (error) {
295293
const errorMessage = firebaseError(error.code, locale);
296-
return dispatch(AUTH_FACEBOOK_FAIL({ error: errorMessage }));
294+
return dispatch(AUTH_PROVIDER_FAIL({ error: errorMessage }));
297295
}
298296
}
299297

300-
return dispatch(AUTH_FACEBOOK_SUCCESS({ id: uid, ...userData }));
298+
return dispatch(
299+
AUTH_PROVIDER_SUCCESS({ id: uid, ...userData, ...userValue })
300+
);
301301
};
302302
};
303+
304+
export const authFacebook = () => {
305+
const provider = new firebase.auth.FacebookAuthProvider();
306+
provider.addScope('email');
307+
provider.addScope('user_location');
308+
return authWithProvider(provider);
309+
};
310+
311+
export const authGoogle = () => {
312+
const provider = new firebase.auth.GoogleAuthProvider();
313+
provider.addScope('https://www.googleapis.com/auth/user.addresses.read');
314+
provider.addScope('https://www.googleapis.com/auth/userinfo.email');
315+
return authWithProvider(provider);
316+
};

src/state/actions/users.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,9 @@ export const fetchUsers = () => {
7373
};
7474

7575
const deleteLogo = oldLogo => {
76+
if (!oldLogo.includes('firebasestorage')) {
77+
return null;
78+
}
7679
const logoPath = oldLogo
7780
.split('users%2F')
7881
.pop()

src/state/reducers/auth/auth.test.js

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,9 @@ import {
2020
AUTH_CHANGE_PASSWORD_SUCCESS,
2121
AUTH_CHANGE_PASSWORD_FAIL,
2222
AUTH_UPDATE_USER_DATA,
23-
AUTH_FACEBOOK_FAIL,
24-
AUTH_FACEBOOK_INIT,
25-
AUTH_FACEBOOK_SUCCESS
23+
AUTH_PROVIDER_FAIL,
24+
AUTH_PROVIDER_INIT,
25+
AUTH_PROVIDER_SUCCESS
2626
} from 'state/actions/auth';
2727

2828
import { authReducer } from '.';
@@ -217,31 +217,31 @@ describe('Auth reducer', () => {
217217
});
218218
});
219219

220-
it('should set loading to true when AUTH_FACEBOOK_INIT action is fired', () => {
221-
reducerTest(initialState, AUTH_FACEBOOK_INIT(), {
220+
it('should set loading to true when AUTH_PROVIDER_INIT action is fired', () => {
221+
reducerTest(initialState, AUTH_PROVIDER_INIT(), {
222222
...initialState,
223223
loading: true
224224
});
225225
});
226226

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

233-
reducerTest(initialState, AUTH_FACEBOOK_SUCCESS(payload), {
233+
reducerTest(initialState, AUTH_PROVIDER_SUCCESS(payload), {
234234
...initialState,
235235
userData: {
236236
...payload
237237
}
238238
});
239239
});
240240

241-
it('should set loading to false and error with the corresponding payload when AUTH_FACEBOOK_FAIL action is fired', () => {
241+
it('should set loading to false and error with the corresponding payload when AUTH_PROVIDER_FAIL action is fired', () => {
242242
reducerTest(
243243
{ ...initialState, loading: true },
244-
AUTH_FACEBOOK_FAIL({ error: 'sample error' }),
244+
AUTH_PROVIDER_FAIL({ error: 'sample error' }),
245245
{ ...initialState, error: 'sample error' }
246246
);
247247
});

src/state/reducers/auth/index.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,9 @@ import {
2222
AUTH_CHANGE_PASSWORD_SUCCESS,
2323
AUTH_CHANGE_PASSWORD_FAIL,
2424
AUTH_UPDATE_USER_DATA,
25-
AUTH_FACEBOOK_FAIL,
26-
AUTH_FACEBOOK_INIT,
27-
AUTH_FACEBOOK_SUCCESS
25+
AUTH_PROVIDER_FAIL,
26+
AUTH_PROVIDER_INIT,
27+
AUTH_PROVIDER_SUCCESS
2828
} from 'state/actions/auth';
2929

3030
const initialState = {
@@ -144,11 +144,11 @@ export const authReducer = createReducer(
144144
createdAt: payload.createdAt
145145
}
146146
}),
147-
[AUTH_FACEBOOK_INIT]: state => ({
147+
[AUTH_PROVIDER_INIT]: state => ({
148148
...state,
149149
loading: true
150150
}),
151-
[AUTH_FACEBOOK_SUCCESS]: (state, payload) => ({
151+
[AUTH_PROVIDER_SUCCESS]: (state, payload) => ({
152152
...state,
153153
userData: {
154154
id: payload.id,
@@ -162,7 +162,7 @@ export const authReducer = createReducer(
162162
error: null,
163163
loading: false
164164
}),
165-
[AUTH_FACEBOOK_FAIL]: (state, payload) => ({
165+
[AUTH_PROVIDER_FAIL]: (state, payload) => ({
166166
...state,
167167
loading: false,
168168
error: payload.error

0 commit comments

Comments
 (0)