Created
January 28, 2021 15:42
-
-
Save premacck/1b79f7f7b5f2c54fb72b18a40afb7845 to your computer and use it in GitHub Desktop.
Script to add users from firestore emulator data to auth emulator
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const admin = require('firebase-admin'); | |
const readline = require('readline'); | |
// Use your project's admin service account JSON, or you can hardcode your project ID in admin.initializeApp below | |
const serviceAccount = require('./your-project-firebase-adminsdk-abcde-1234567890.json'); | |
const rl = readline.createInterface({ | |
input: process.stdin, | |
output: process.stdout, | |
}); | |
admin.initializeApp({ projectId: serviceAccount.project_id }); | |
// Function to put data in firebase auth from firestore | |
(async () => { | |
const auth = admin.auth(); | |
const db = admin.firestore(); | |
const usersSnapshot = await db.collectionGroup('all_users').get(); | |
usersSnapshot.forEach(async (doc) => { | |
const data = doc.data(); | |
if (data.email) { | |
console.log(`Auto creating user ${data.name} with email ${data.email} in auth emulator`); | |
await auth.createUser({ | |
uid: data.uid, | |
email: data.email, | |
password: '123456', | |
displayName: data.name, | |
emailVerified: data.is_email_verified, | |
}); | |
// Optional, if you're setting custom user claims in your case | |
await auth.setCustomUserClaims(data.uid, { is_admin: data.is_admin }); | |
} | |
}); | |
// Used to keep firebase emulator alive | |
rl.question('Press CTRL+C to shit down emulator', () => { }); | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment