Skip to content

Commit 95d0c56

Browse files
committed
fix: avoid overwriting existing osm profiles on sign in
1 parent 3944e67 commit 95d0c56

File tree

2 files changed

+35
-12
lines changed

2 files changed

+35
-12
lines changed

firebase/functions/src/osm_auth.ts

Lines changed: 34 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -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;

mapswipe_workers/requirements.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ firebase-admin==6.0.0
55
flake8==3.8.3
66
geojson==3.0.1
77
mapswipe-workers==3.0
8+
numpy==1.26.4
89
pandas==1.5.2
910
pre-commit==2.9.2
1011
psycopg2-binary==2.9.3

0 commit comments

Comments
 (0)