Skip to content

Commit 799c4fc

Browse files
author
Scott Prue
committed
profileParamsToPopulate config option. AvatarUrl parameter added to default profile.
* profileParamsToPopulate config option added and working * avatarUrl parameter added to profile object for provider logins
1 parent ddc180c commit 799c4fc

File tree

3 files changed

+49
-11
lines changed

3 files changed

+49
-11
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: 46 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,29 @@ const unWatchUserProfile = (firebase) => {
8383
firebase._.profileWatch = null
8484
}
8585
}
86-
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+
}
87109
/**
88110
* @description Watch user profile
89111
* @param {Function} dispatch - Action dispatch function
@@ -93,15 +115,33 @@ const watchUserProfile = (dispatch, firebase) => {
93115
const authUid = firebase._.authUid
94116
const userProfile = firebase._.config.userProfile
95117
unWatchUserProfile(firebase)
118+
96119
if (firebase._.config.userProfile) {
97120
firebase._.profileWatch = firebase.database()
98121
.ref()
99122
.child(`${userProfile}/${authUid}`)
100123
.on('value', snap => {
101-
dispatch({
102-
type: SET_PROFILE,
103-
profile: snap.val()
104-
})
124+
const { profileParamsToPopulate } = firebase._.config
125+
if (!profileParamsToPopulate || (!isArray(profileParamsToPopulate) && !isString(profileParamsToPopulate))) {
126+
dispatch({
127+
type: SET_PROFILE,
128+
profile: snap.val()
129+
})
130+
} else {
131+
// Handle string and array for profileParamsToPopulate config option
132+
const paramsToPopulate = isArray(firebase._.config.profileParamsToPopulate)
133+
? firebase._.config.profileParamsToPopulate
134+
: firebase._.config.profileParamsToPopulate.split(',')
135+
136+
// 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+
})
143+
})
144+
}
105145
})
106146
}
107147
}
@@ -244,6 +284,7 @@ export const login = (dispatch, firebase, credentials) => {
244284
{
245285
email: user.email,
246286
displayName: user.providerData[0].displayName || user.email,
287+
avatarUrl: user.providerData[0].photoURL,
247288
providerData: user.providerData
248289
}
249290
)

0 commit comments

Comments
 (0)