-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathfirebase_users.js
52 lines (42 loc) · 1.66 KB
/
firebase_users.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
const admin = require("firebase-admin");
// Initialize Firebase Admin SDK with the service account
// Since we did not add user details to firestore
// this script takes details from firebase authentication
// and then updates the firestore document with name and profileImage
const serviceAccount = require("./serviceAccountKey.json");
admin.initializeApp({
credential: admin.credential.cert(serviceAccount),
});
const firestore = admin.firestore();
const auth = admin.auth();
async function updateUserProfiles() {
const usersSnapshot = await firestore.collection("users").get();
console.log(`Fetched ${usersSnapshot.size} users.`);
for (const doc of usersSnapshot.docs) {
const userData = doc.data();
const uid = doc.id;
// Check if fields are missing
if (!userData.name || !userData.profileImage) {
try {
// Get user data from Firebase Authentication
const userRecord = await auth.getUser(uid);
const updates = {};
if (!userData.name) {
updates.name = userRecord.displayName || "Anonymous";
}
if (!userData.profileImage) {
updates.profileImage = userRecord.photoURL || "default_image_url";
}
// Update the Firestore document
await firestore.collection("users").doc(uid).update(updates);
console.log(`Updated user ${uid} with data:`, updates);
} catch (error) {
console.error(`Error updating user ${uid}:`, error);
}
} else {
console.log(`User ${uid} already has name and profile image.`);
}
}
console.log("User profile update process completed.");
}
updateUserProfiles().then(() => console.log("User profiles updated."));