Created
November 12, 2019 18:28
-
-
Save bchiang7/add8fb67d7d3dd7213ae0d97f1007ece to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| // 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