-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbulkImportUsers.js
64 lines (59 loc) · 1.88 KB
/
bulkImportUsers.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
require('dotenv').config()
const fs = require('fs')
const firebaseAdmin = require('firebase-admin')
const FIREBASE_PRIVATE_KEY = process.env.FIREBASE_PRIVATE_KEY
// Initialize Firebase
if (!firebaseAdmin.apps.length) {
firebaseAdmin.initializeApp({
credential: firebaseAdmin.credential.cert({
projectId: process.env.FIREBASE_PROJECT_ID,
clientEmail: process.env.FIREBASE_CLIENT_EMAIL,
privateKey: FIREBASE_PRIVATE_KEY.replace(/\\n/g, '\n'),
}),
databaseURL: process.env.FIREBASE_DATABASE_URL,
});
}
// Collection name you are going to import into
const FIREBASE_COLLECTION = 'users'
// Read user data from JSON file
const userData = fs.readFileSync('users.json', 'utf-8');
const users = JSON.parse(userData);
const firestore = firebaseAdmin.firestore()
users.map((user) => {
firestore.collection(FIREBASE_COLLECTION).doc(user.id).set({
displayName: user.firstname + ' ' + user.lastname,
email: user.email,
hasLifetimeAccess: false,
stripeCustomerId: user.stripe_customer_id,
stripeSubscriptionId: user.stripe_subscription_id,
stripePriceId: 'price_XXXXXXXXXXXXXXXXXXXXXXX',
stripeSubscriptionStatus: 'active',
}).then(() => {
console.log('Document successfully written!');
}).catch((error) => {
console.error('Error writing document: ', error);
});
});
// Generate an object with the correct format for importing into Firebase
const mapUsers = users.map((user) => {
return {
uid: user.id,
email: user.email,
passwordHash: Buffer.from(user.pwd),
};
});
firebaseAdmin.auth().importUsers(
mapUsers,
{
hash: {
algorithm: 'BCRYPT'
}
}
)
.then((userRecord) => {
console.log('Successfully imported ', userRecord.successCount, ' users with ', userRecord.failureCount, ' failures');
console.log('Firebase errors: ', userRecord.errors);
})
.catch((error) => {
console.error('Error importing user:', error);
});