@@ -10,6 +10,7 @@ import {
1010} from '../constants'
1111import { capitalize , omit , isArray , isString , isFunction } from 'lodash'
1212import 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 ) {
0 commit comments