Skip to content

Instantly share code, notes, and snippets.

@Vann-Dev
Created August 29, 2023 19:12
Show Gist options
  • Save Vann-Dev/59c97592404e1b79d3b423ec55746950 to your computer and use it in GitHub Desktop.
Save Vann-Dev/59c97592404e1b79d3b423ec55746950 to your computer and use it in GitHub Desktop.
/**
* Made By Vann-Dev
*
* IMPORT USER & UN ENCRYPTED PASSWORD TO FIREBASE AUTH
*/
const admin = require("firebase-admin");
/**
* Path to ServiceAccount
*/
const serviceAccount = require("./tempService.json");
const bcypt = require('bcrypt');
admin.initializeApp({
credential: admin.credential.cert(serviceAccount)
});
/**
* Path to user data json
*/
const allUsers = require("./production.User.json")
const batchSize = 1000;
const userBatches = [];
for (let i = 0; i < allUsers.length; i += batchSize) {
userBatches.push(allUsers.slice(i, i + batchSize));
}
async function importUserBatch(userBatch) {
const salt = bcypt.genSaltSync()
console.log("Updating: " + userBatch.length)
const userRecords = userBatch.map(user => ({
uid: user._id.$oid,
email: user.email,
passwordHash: Buffer.from(bcypt.hashSync(user.password, salt))
}));
console.log("Uploading: " + userRecords.length)
try {
const result = await admin.auth().importUsers(userRecords, {
hash: {
algorithm: 'BCRYPT',
},
});
console.log('Successfully imported', result.successCount, 'users');
if (result.failureCount > 0) {
console.log('Failed to import', result.failureCount, 'users');
}
} catch (error) {
console.error('Error importing users:', error);
}
}
// Import each batch
(async function () {
console.log("Running")
for (const userBatch of userBatches) {
await importUserBatch(userBatch);
}
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment