This repository was archived by the owner on Mar 26, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtestImport.js
More file actions
60 lines (53 loc) · 1.75 KB
/
testImport.js
File metadata and controls
60 lines (53 loc) · 1.75 KB
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
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()
// uid must match the id passed in doc() below
firestore.collection(FIREBASE_COLLECTION).doc('4816521231231').set({
displayName: 'Joe Bloggs',
email: 'joe.bloggs@gmail.com',
}).then(() => {
console.log('Document successfully written!');
}).catch((error) => {
console.error('Error writing document: ', error);
});
firebaseAdmin.auth().importUsers(
// Use the below to test importing a single user
// Once you can confirm that works, then you can use the bulkImportUsers.js script
[
{
uid: '4816521231231',
email: 'joe.bloggs@gmail.com',
passwordHash: Buffer.from("$2a$12$ptzSDrLIW.A/2BNS9TKC6urWY7sOpdv2Bm3rcpWGpBgmi3UuiK9ri"),
}
],
{
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);
});