Skip to content

Commit 21fc6ce

Browse files
authored
v1.0.3
* `profileParamsToPopulate` capability added to config * `avatarUrl` parameter added to default profile for external provider logins * `logout()` method now returns a promise * Profile created by default when not provided for email signup * [`lodash`](https://www.npmjs.com/package/lodash) updated to v4.16.6 * [`firebase`](https://www.npmjs.com/package/firebase) updated to v3.5.3
2 parents ddc180c + b52bb7f commit 21fc6ce

File tree

4 files changed

+71
-12
lines changed

4 files changed

+71
-12
lines changed

.eslintrc

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,5 @@
1919
},
2020
"rules": {
2121
"semi" : [2, "never"],
22-
"max-len": "off",
23-
"generator-star-spacing": 0,
24-
"babel/generator-star-spacing": 1
2522
}
2623
}

package.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "react-redux-firebase",
3-
"version": "1.0.2",
3+
"version": "1.0.3",
44
"description": "Redux integration for Firebase. Comes with a Higher Order Component for use with React.",
55
"main": "dist/index.js",
66
"module": "src/index.js",
@@ -39,10 +39,10 @@
3939
}
4040
],
4141
"dependencies": {
42-
"firebase": "^3.5.2",
42+
"firebase": "^3.5.3",
4343
"immutable": "^3.8.1",
4444
"jwt-decode": "^2.1.0",
45-
"lodash": "^4.16.4"
45+
"lodash": "^4.16.6"
4646
},
4747
"peerDependencies": {
4848
"react": "^0.14.6 || ^15.0.0",

src/actions/auth.js

Lines changed: 36 additions & 6 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
@@ -93,15 +94,41 @@ const watchUserProfile = (dispatch, firebase) => {
9394
const authUid = firebase._.authUid
9495
const userProfile = firebase._.config.userProfile
9596
unWatchUserProfile(firebase)
97+
9698
if (firebase._.config.userProfile) {
9799
firebase._.profileWatch = firebase.database()
98100
.ref()
99101
.child(`${userProfile}/${authUid}`)
100102
.on('value', snap => {
101-
dispatch({
102-
type: SET_PROFILE,
103-
profile: snap.val()
104-
})
103+
const { profileParamsToPopulate } = firebase._.config
104+
if (!profileParamsToPopulate || (!isArray(profileParamsToPopulate) && !isString(profileParamsToPopulate))) {
105+
dispatch({
106+
type: SET_PROFILE,
107+
profile: snap.val()
108+
})
109+
} else {
110+
// Handle string and array for profileParamsToPopulate config option
111+
const paramsToPopulate = isArray(firebase._.config.profileParamsToPopulate)
112+
? firebase._.config.profileParamsToPopulate
113+
: firebase._.config.profileParamsToPopulate.split(',')
114+
115+
// Convert each populate string in array into an array of once query promises
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+
)
129+
})
130+
})
131+
}
105132
})
106133
}
107134
}
@@ -172,8 +199,9 @@ export const createUserProfile = (dispatch, firebase, userData, profile) =>
172199
.child(`${firebase._.config.userProfile}/${userData.uid}`)
173200
.once('value')
174201
.then(profileSnap =>
202+
// update profile only if doesn't exist or if set by config
175203
!firebase._.config.updateProfileOnLogin && profileSnap.val() !== null
176-
? profile
204+
? profileSnap.val()
177205
: profileSnap.ref.update(profile) // Update the profile
178206
.then(() => profile)
179207
.catch(err => {
@@ -244,6 +272,7 @@ export const login = (dispatch, firebase, credentials) => {
244272
{
245273
email: user.email,
246274
displayName: user.providerData[0].displayName || user.email,
275+
avatarUrl: user.providerData[0].photoURL,
247276
providerData: user.providerData
248277
}
249278
)
@@ -271,6 +300,7 @@ export const logout = (dispatch, firebase) => {
271300
dispatch({ type: LOGOUT })
272301
firebase._.authUid = null
273302
unWatchUserProfile(firebase)
303+
return Promise.resolve(firebase)
274304
}
275305

276306
/**
@@ -295,7 +325,7 @@ export const createUser = (dispatch, firebase, { email, password, signIn }, prof
295325
firebase.auth().currentUser || (!!signIn && signIn === false)
296326
? createUserProfile(dispatch, firebase, userData, profile)
297327
: login(dispatch, firebase, { email, password })
298-
.then(() => createUserProfile(dispatch, firebase, userData, profile))
328+
.then(() => createUserProfile(dispatch, firebase, userData, profile || { email }))
299329
.catch(err => {
300330
if (err) {
301331
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)