Forked from cliffhall/nuke-firebase-db-and-users-referenced-therein.js
Created
July 1, 2017 17:14
-
-
Save cyakimov/8504d3b338737c7420d323f26592d3ec to your computer and use it in GitHub Desktop.
Nuke a Firebase Database and All User Accounts Referenced Therein
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
// NOTE: Accounts that are not represented in your /users node will not be deleted! | |
// BLOG: There are other approaches, see: http://cliffordhall.com/2017/04/nuke-firebase-db-and-all-users/ | |
"use strict"; | |
// Get credentials and initialize firebase app | |
console.log("With the power vested in the admin user, lay waste the database and all its users!"); | |
let admin = require("firebase-admin"); | |
let serviceAccount = require([__dirname, "service-account-key.json"].join('/')); | |
admin.initializeApp({ | |
credential: admin.credential.cert(serviceAccount), | |
databaseURL: "https://your-db-name-here.firebaseio.com" | |
}); | |
// Fetch the /users node and begin deleting users. | |
// Finally, wipe the database. | |
let user, users = []; | |
let usersRef = admin.database().ref('/users'); | |
usersRef.once('value').then( (snapshot) => { | |
// Get an Array of users from the DatabaseSnapshot | |
snapshot.forEach( (childSnapshot) => { | |
user = childSnapshot.val(); | |
users.push(user); | |
}); | |
console.log(users.length + " users retrieved"); | |
// Delete users then wipe the database | |
if (users.length > 0) { | |
// Now map users to an Array of Promises | |
console.log("Delete users... "); | |
let promises = users.map(user => deleteUser(user)); | |
// Wait for all Promises to complete before wiping db | |
Promise.all(promises) | |
.then(wipeDatabase) | |
.catch( e => console.log(e.message) ); | |
} else { | |
// If no users to delete, then just wipe database | |
wipeDatabase(); | |
} | |
// Delete a user | |
// Promise always resolves, even if user doesn't exist, | |
// since a user can be deleted separately from their /users node | |
function deleteUser(user) { | |
return new Promise((resolve, reject) => { | |
console.log("Delete user: " + user.name + ""); | |
admin.auth() | |
.deleteUser(user.uid) | |
.then( () => { | |
console.log(user.name + " deleted."); | |
resolve(user); | |
}) | |
.catch( e => { | |
console.log([e.message, user.name, "could not be deleted!"].join(' ')); | |
resolve(user); | |
}); | |
}); | |
} | |
// Wipe the database by removing the root node | |
function wipeDatabase() { | |
console.log("Wiping database... "); | |
admin.database().ref().remove() | |
.then( () => { | |
console.log('DONE!'); | |
process.exit(); | |
}) | |
.catch( e => { | |
console.log(e.message); | |
process.exit(); | |
}); | |
} | |
}); |
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
{ | |
"type": "service_account", | |
"project_id": "your-db-name-here", | |
"private_key_id": "b4158530b6255bd098369aa94f2313a60e1aa534", | |
"private_key": "-----BEGIN PRIVATE KEY-----\nyour-private-key-here\n-----END PRIVATE KEY-----\n", | |
"client_email": "[email protected]", | |
"client_id": "your-client-id-here", | |
"auth_uri": "https://accounts.google.com/o/oauth2/auth", | |
"token_uri": "https://accounts.google.com/o/oauth2/token", | |
"auth_provider_x509_cert_url": "https://www.googleapis.com/oauth2/v1/certs", | |
"client_x509_cert_url": "https://www.googleapis.com/robot/v1/metadata/x509/firebase-adminsdk-xxxx%40cyour-db-name-here.iam.gserviceaccount.com" | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment