-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
102 lines (89 loc) · 3.35 KB
/
index.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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
const admin = require('firebase-admin');
const fs = require('fs');
const csvWriter = require('csv-writer').createObjectCsvWriter;
//firebase admin configure
const serviceAccount = require('./serviceAccountKey.json');
const outputfile = 'imported_users.csv';
const { createObjectCsvWriter } = require('csv-writer');
const readline = require('readline');
let importedCount = 0;
admin.initializeApp({
credential: admin.credential.cert(serviceAccount)
});
// Function to list all users and write to CSV
const listAllUsers = async (nextPageToken) => {
try {
const listUsersResult = await admin.auth().listUsers(1000, nextPageToken);
const users = listUsersResult.users.map((userRecord) => {
let loginType = 'Anonymous';
if (userRecord.providerData && userRecord.providerData.length > 0) {
const provider = userRecord.providerData[0].providerId;
if (provider === 'google.com') {
loginType = 'Google';
} else if (provider === 'apple.com') {
loginType = 'Apple';
} else if (provider === 'facebook.com') {
loginType = 'Facebook';
} else if (provider === 'password') {
loginType = 'Email/Password';
} else if (provider === 'phone') {
loginType = 'Phone';
} else if (provider === 'twitter.com') {
loginType = 'Twitter';
} else if (provider === 'github.com') {
loginType = 'Github';
} else if (provider === 'microsoft.com') {
loginType = 'Microsoft';
} else if (provider === 'yahoo.com') {
loginType = 'Yahoo';
} else if (provider === 'linkedin.com') {
loginType = 'LinkedIn';
}
}
return {
uid: userRecord.uid,
email: userRecord.email,
displayName: userRecord.displayName,
phoneNumber: userRecord.phoneNumber,
createdAt: userRecord.metadata.creationTime,
loginType: loginType,
};
});
const filteredUsers = users.filter(user => user.email);
const csvWriter = createObjectCsvWriter({
path: outputfile,
header: [
{ id: 'uid', title: 'UID' },
{ id: 'email', title: 'Email' },
{ id: 'displayName', title: 'Display Name' },
{ id: 'phoneNumber', title: 'Phone Number' },
{ id: 'createdAt', title: 'Created At' },
{ id: 'loginType', title: 'Login Type' },
],
append: true, // Append records to the existing file
});
for (const user of filteredUsers) {
await csvWriter.writeRecords([user]);
importedCount++;
const spinner = ['|', '/', '-', '\\'];
let spinnerIndex = 0;
const animateActivity = () => {
process.stdout.write(` Imported ${importedCount} email users ${spinner[spinnerIndex]}\r`);
spinnerIndex = (spinnerIndex + 1) % spinner.length;
};
setInterval(animateActivity, 100);
}
if (listUsersResult.pageToken) {
// List next batch of users.
await listAllUsers(listUsersResult.pageToken);
}
} catch (error) {
console.log('Error listing users:', error);
}
};
//Delete outputfile if exists
if (fs.existsSync(outputfile)) {
fs.unlinkSync(outputfile);
}
// Start listing users from the beginning, 1000 at a time.
listAllUsers();