Skip to content

Instantly share code, notes, and snippets.

@bchiang7
Created November 12, 2019 18:28
Show Gist options
  • Select an option

  • Save bchiang7/add8fb67d7d3dd7213ae0d97f1007ece to your computer and use it in GitHub Desktop.

Select an option

Save bchiang7/add8fb67d7d3dd7213ae0d97f1007ece to your computer and use it in GitHub Desktop.
// Deletes a user by ID in firestore user collection & firebase auth
static async deleteUserById(id) {
const userRef = firestore
.collection(this.collectionName())
.doc(id);
const batch = firestore.batch();
batch.delete(userRef);
const completedTasksToDelete = await firestore
.collection('completedTasks')
.where('userId', '==', id)
.get();
const savedTasksToDelete = await firestore
.collection('savedTasks')
.where('userId', '==', id)
.get();
completedTasksToDelete.docs.forEach(doc => batch.delete(doc.ref));
savedTasksToDelete.docs.forEach(doc => batch.delete(doc.ref));
try {
// Delete the user record and all other records related to it
await batch.commit();
// Delete the user from the auth table
await admin.auth().deleteUser(id);
} catch (e) {
throw new Error('Could not delete user.');
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment