Skip to content

Commit b52bb7f

Browse files
author
Scott Prue
committed
promisesForPopulate moved to utils. Logout returns promise.
1 parent 799c4fc commit b52bb7f

File tree

2 files changed

+52
-31
lines changed

2 files changed

+52
-31
lines changed

src/actions/auth.js

Lines changed: 20 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import {
1010
} from '../constants'
1111
import { capitalize, omit, isArray, isString, isFunction } from 'lodash'
1212
import jwtDecode from 'jwt-decode'
13+
import { promisesForPopulate } from '../utils'
1314

1415
/**
1516
* @description Dispatch login error action
@@ -83,29 +84,7 @@ const unWatchUserProfile = (firebase) => {
8384
firebase._.profileWatch = null
8485
}
8586
}
86-
const promisesForPopulate = (firebase, profile, populateString) => {
87-
const paramToPopulate = populateString.split(':')[0]
88-
const populateRoot = populateString.split(':')[1]
89-
let idList = profile[paramToPopulate]
90-
if (isString(profile[paramToPopulate])) {
91-
idList = profile[paramToPopulate].split(',')
92-
}
93-
return Promise.all(
94-
idList.map(itemId =>
95-
firebase.database()
96-
.ref()
97-
.child(populateRoot)
98-
.child(itemId)
99-
.once('value')
100-
.then(snap => snap.val() || itemId)
101-
)).then(data => {
102-
const populatedObj = {}
103-
idList.forEach(item => populatedObj)
104-
populatedObj[paramToPopulate] = data
105-
return populatedObj
106-
}
107-
)
108-
}
87+
10988
/**
11089
* @description Watch user profile
11190
* @param {Function} dispatch - Action dispatch function
@@ -134,13 +113,21 @@ const watchUserProfile = (dispatch, firebase) => {
134113
: firebase._.config.profileParamsToPopulate.split(',')
135114

136115
// Convert each populate string in array into an array of once query promises
137-
Promise.all(paramsToPopulate.map(p => promisesForPopulate(firebase, snap.val(), p)))
138-
.then((data) => {
139-
dispatch({
140-
type: SET_PROFILE,
141-
profile: Object.assign(snap.val(), data.reduce((a, b) => Object.assign(a, b)))
142-
})
116+
Promise.all(
117+
paramsToPopulate.map(p =>
118+
promisesForPopulate(firebase, snap.val(), p)
119+
)
120+
)
121+
.then(data => {
122+
// Dispatch action with profile combined with populated parameters
123+
dispatch({
124+
type: SET_PROFILE,
125+
profile: Object.assign(
126+
snap.val(), // profile
127+
data.reduce((a, b) => Object.assign(a, b)) // populated profile parameters
128+
)
143129
})
130+
})
144131
}
145132
})
146133
}
@@ -212,8 +199,9 @@ export const createUserProfile = (dispatch, firebase, userData, profile) =>
212199
.child(`${firebase._.config.userProfile}/${userData.uid}`)
213200
.once('value')
214201
.then(profileSnap =>
202+
// update profile only if doesn't exist or if set by config
215203
!firebase._.config.updateProfileOnLogin && profileSnap.val() !== null
216-
? profile
204+
? profileSnap.val()
217205
: profileSnap.ref.update(profile) // Update the profile
218206
.then(() => profile)
219207
.catch(err => {
@@ -312,6 +300,7 @@ export const logout = (dispatch, firebase) => {
312300
dispatch({ type: LOGOUT })
313301
firebase._.authUid = null
314302
unWatchUserProfile(firebase)
303+
return Promise.resolve(firebase)
315304
}
316305

317306
/**
@@ -336,7 +325,7 @@ export const createUser = (dispatch, firebase, { email, password, signIn }, prof
336325
firebase.auth().currentUser || (!!signIn && signIn === false)
337326
? createUserProfile(dispatch, firebase, userData, profile)
338327
: login(dispatch, firebase, { email, password })
339-
.then(() => createUserProfile(dispatch, firebase, userData, profile))
328+
.then(() => createUserProfile(dispatch, firebase, userData, profile || { email }))
340329
.catch(err => {
341330
if (err) {
342331
switch (err.code) {

src/utils/index.js

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import { isString } from 'lodash'
2+
3+
/**
4+
* @description Watch user profile
5+
* @param {Function} dispatch - Action dispatch function
6+
* @param {Object} firebase - Internal firebase object
7+
*/
8+
export const promisesForPopulate = (firebase, profile, populateString) => {
9+
const paramToPopulate = populateString.split(':')[0]
10+
const populateRoot = populateString.split(':')[1]
11+
let idList = profile[paramToPopulate]
12+
if (isString(profile[paramToPopulate])) {
13+
idList = profile[paramToPopulate].split(',')
14+
}
15+
return Promise.all(
16+
idList.map(itemId =>
17+
firebase.database()
18+
.ref()
19+
.child(populateRoot)
20+
.child(itemId)
21+
.once('value')
22+
.then(snap => snap.val() || itemId)
23+
)).then(data => {
24+
const populatedObj = {}
25+
idList.forEach(item => populatedObj)
26+
populatedObj[paramToPopulate] = data
27+
return populatedObj
28+
}
29+
)
30+
}
31+
32+
export default { promisesForPopulate }

0 commit comments

Comments
 (0)