0
votes

As I understand, it is not possible for one user to delete another user in the firebase. From previous topic I learn that I can use firebase functions for that. Each user has a document in the cloud firebase (path: /users/userPhoneNumber/{age,height,...}). Once the document is deleted, I want to delete the user from the firebase authentication. I know how to catch a change in the cloud firebase using function (although I'm not sure how to catch a deletion), but the problem I'm having is how can I delete the user? I'm using Java for my app side and javascript for my funcations side. As I understand, the user should have the app installed on the phone in order to delete his authentication.

1
So, on deletion of the user document in the Firestore users collection, you want to delete the user in the Auth service. Is that right? What is the id of the document in the users collection? the user phone number? - Renaud Tarnec
@RenaudTarnec Yes you understood correctly. As I understand it's the only way to delete the user from the Auth service (if not, please tell me how). The document id is user's phone number. - vesii
The clients SDKs offer a method for deleting the user (see here for the JS SDK) but only the signed in user can delete his own account, not another user. - Renaud Tarnec

1 Answers

3
votes

Since the user's Firestore document ID is the user's phone number, you can write a Cloud Function as follows, by using the Admin SDK getUserByPhoneNumber() and deleteUser() methods.

exports.deleteUser = functions.firestore
    .document('users/{userPhoneNbr}')
    .onDelete(async (snap, context) => {
        
        try {
            
            const userPhoneNbr = context.params.userPhoneNbr;
            const userRecord = await admin.auth().getUserByPhoneNumber(userPhoneNbr);
            await admin.auth().deleteUser(userRecord.uid);
            return null;
            
        } catch (error) {
            
            // ....
            
        }

    });