@@ -10,6 +10,7 @@ import {
10
10
} from '../constants'
11
11
import { capitalize , omit , isArray , isString , isFunction } from 'lodash'
12
12
import jwtDecode from 'jwt-decode'
13
+ import { promisesForPopulate } from '../utils'
13
14
14
15
/**
15
16
* @description Dispatch login error action
@@ -93,15 +94,41 @@ const watchUserProfile = (dispatch, firebase) => {
93
94
const authUid = firebase . _ . authUid
94
95
const userProfile = firebase . _ . config . userProfile
95
96
unWatchUserProfile ( firebase )
97
+
96
98
if ( firebase . _ . config . userProfile ) {
97
99
firebase . _ . profileWatch = firebase . database ( )
98
100
. ref ( )
99
101
. child ( `${ userProfile } /${ authUid } ` )
100
102
. 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
+ }
105
132
} )
106
133
}
107
134
}
@@ -172,8 +199,9 @@ export const createUserProfile = (dispatch, firebase, userData, profile) =>
172
199
. child ( `${ firebase . _ . config . userProfile } /${ userData . uid } ` )
173
200
. once ( 'value' )
174
201
. then ( profileSnap =>
202
+ // update profile only if doesn't exist or if set by config
175
203
! firebase . _ . config . updateProfileOnLogin && profileSnap . val ( ) !== null
176
- ? profile
204
+ ? profileSnap . val ( )
177
205
: profileSnap . ref . update ( profile ) // Update the profile
178
206
. then ( ( ) => profile )
179
207
. catch ( err => {
@@ -244,6 +272,7 @@ export const login = (dispatch, firebase, credentials) => {
244
272
{
245
273
email : user . email ,
246
274
displayName : user . providerData [ 0 ] . displayName || user . email ,
275
+ avatarUrl : user . providerData [ 0 ] . photoURL ,
247
276
providerData : user . providerData
248
277
}
249
278
)
@@ -271,6 +300,7 @@ export const logout = (dispatch, firebase) => {
271
300
dispatch ( { type : LOGOUT } )
272
301
firebase . _ . authUid = null
273
302
unWatchUserProfile ( firebase )
303
+ return Promise . resolve ( firebase )
274
304
}
275
305
276
306
/**
@@ -295,7 +325,7 @@ export const createUser = (dispatch, firebase, { email, password, signIn }, prof
295
325
firebase . auth ( ) . currentUser || ( ! ! signIn && signIn === false )
296
326
? createUserProfile ( dispatch , firebase , userData , profile )
297
327
: login ( dispatch , firebase , { email, password } )
298
- . then ( ( ) => createUserProfile ( dispatch , firebase , userData , profile ) )
328
+ . then ( ( ) => createUserProfile ( dispatch , firebase , userData , profile || { email } ) )
299
329
. catch ( err => {
300
330
if ( err ) {
301
331
switch ( err . code ) {
0 commit comments