Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Script to add users from firestore emulator data to auth emulator
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