Delete a specific user from Firebase

后端 未结 4 1956
北荒
北荒 2020-11-29 08:34

Is there a way I can get a specific user account from firebase and then delete it?

For instance:

// I need a means of getting a specific auth user.
v         


        
4条回答
  •  醉话见心
    2020-11-29 08:42

    I know this is an old question, but I found another solution to this. You definitely don't want to use firebase-admin in your application itself, as I think was suggested by Ali Haider, since it needs a private key which you don't want to deploy with your code.

    You can however create a Cloud Function in Firebase that triggers on the deletion of a user in your Firestore or Realtime database and let that Cloud Function use firebase-admin to delete the user. In my case I have a collection of users in my Firestore with the same userid's as created by Firebase Auth, in which I save extra user data like the name and the role etc.

    If you're using Firestore as me, you can do the following. If you're using Realtime database, just look up in the documentation how to use a trigger for that.

    1. Make sure your Firebase project has cloud functions initialized. There should be a folder named 'functions' in your project directory. If not: initialize Cloud Functions for your project with the following command: firebase init functions.

    2. Obtain a private key for your service account in the Firebase Console on the following page: Settings > Service accounts.

    3. Place the json-file containing the private key in the functions\src folder next to the index.ts file.

    4. Export the following function in index.ts:

    export const removeUser = functions.firestore.document("/users/{uid}")
        .onDelete((snapshot, context) => {        
            const serviceAccount = require('path/to/serviceAccountKey.json');
            admin.initializeApp({
                credential: admin.credential.cert(serviceAccount),
                databaseURL: "https://>.firebaseio.com"
            });
            return admin.auth().deleteUser(context.params.uid);
        });
    
    1. Now deploy your Cloud Function with the command firebase deploy --only functions

    When a user is deleted in your Firebase Firestore, this code will run and also delete the user from Firebase Auth.

    For more information on Firebase Cloud Functions, see https://firebase.google.com/docs/functions/get-started

提交回复
热议问题