i need to deleete an user from firebase auth, and then delete the document corresponding to the user.
The deletion from authentication works, but i dont know how to delete from firestore. I have this
import * as functions from "firebase-functions";
import * as admin from "firebase-admin";
admin.initializeApp();
export const deleteUser = functions.https.onRequest(async (request, response) =>{
const userEmail = request.body.userEmail;
const collection = request.body.collection;
let uid = '';
admin.auth().getUserByEmail(userEmail)
.then(userRecord => {
uid = userRecord.uid
admin.auth().deleteUser(uid)
})
.then( () => {
admin.firestore().collection(collection).doc(uid).get()
.then(queryResult => {
queryResult.ref.delete()
})
response.status(200).send('deleted')
})
.catch(error => {
response.status(500).send('Failed')
})
})
To try i used postman with
{
"userEmail": "[email protected]",
"collection" : "Users"
}
2
Answers
All you have to do is get the doc ref and call the delete function. I leave error handling up to you.
Read the docs: https://firebase.google.com/docs/firestore/manage-data/delete-data#web-version-8
In addition to what
@kingkong.js
mentioned in their answer (to useDocumentReference#delete()
), you also need to ensure your Promise chains are wired up correctly. If not, your function will be terminated before it makes any changes.You may want to consider using a Callable Cloud Function (
functions.https.onCall
) instead as it can handle managing the request body, response and authentication for you.