@@ -236,23 +236,19 @@ async function createFirebaseAccount(admin: any, osmID: any, displayName: any, a
236236 // with a variable length.
237237 const uid = `osm:${ osmID } ` ;
238238
239+ // check if profile exists on Firebase Realtime Database
240+ const profileExists = await admin
241+ . database ( )
242+ . ref ( `v2/users/osm:${ id } /` )
243+ . get ( )
244+ . then ( ( snapshot : any ) => { return snapshot . exists ( ) } ) ;
245+
239246 // Save the access token to the Firebase Realtime Database.
240247 const databaseTask = admin
241248 . database ( )
242249 . ref ( `v2/OSMAccessToken/${ uid } ` )
243250 . set ( accessToken ) ;
244251
245- const profileTask = admin
246- . database ( )
247- . ref ( `v2/users/${ uid } /` )
248- . set ( {
249- created : new Date ( ) . toISOString ( ) ,
250- groupContributionCount : 0 ,
251- projectContributionCount : 0 ,
252- taskContributionCount : 0 ,
253- displayName,
254- } ) ;
255-
256252 // Create or update the firebase user account.
257253 // This does not login the user on the app, it just ensures that a firebase
258254 // user account (linked to the OSM account) exists.
@@ -272,8 +268,34 @@ async function createFirebaseAccount(admin: any, osmID: any, displayName: any, a
272268 throw error ;
273269 } ) ;
274270
271+ // Only update display name if profile exists, else create profile
272+ const profileUpdateTask = admin
273+ . database ( )
274+ . ref ( `v2/users/${ uid } /` )
275+ . update ( { displayName } )
276+
277+ const profileCreationTask = admin
278+ . database ( )
279+ . ref ( `v2/users/${ uid } /` )
280+ . set ( {
281+ created : new Date ( ) . toISOString ( ) ,
282+ groupContributionCount : 0 ,
283+ projectContributionCount : 0 ,
284+ taskContributionCount : 0 ,
285+ displayName,
286+ } ) ;
287+ }
288+
289+ const tasks = [ userCreationTask , databaseTask ]
290+
291+ if ( profileExists ) {
292+ tasks . push ( profileUpdateTask )
293+ } else {
294+ tasks . push ( profileCreationTask )
295+ }
296+
275297 // Wait for all async task to complete then generate and return a custom auth token.
276- await Promise . all ( [ userCreationTask , databaseTask , profileTask ] ) ;
298+ await Promise . all ( tasks ) ;
277299 // Create a Firebase custom auth token.
278300 functions . logger . log ( 'In createFirebaseAccount: createCustomToken' ) ;
279301 let authToken ;
0 commit comments