How to bulk delete Firebase anonymous users

后端 未结 7 2068
小鲜肉
小鲜肉 2020-12-16 03:27

Due to my probable misuse of anonymous authentication (see How to prevent Firebase anonymous user token from expiring) I have a lot of anonymous users in my app that I don\'

7条回答
  •  醉梦人生
    2020-12-16 04:24

    I faced the same problem today then I found Firebase Admin SDK. I am using Node.js which is very easy to install, so you can try the following code. It is not a complete answer I know but one can build its own script/application to delete stored uids. Yet, there is no way to retrieve a list, so you have to build one somehow every time you create an anonymous account.

    First, download your 'serviceAccountKey.json' which can be done through the Firebase Console (Project Settings). In my case I renamed the download file to a more friendly name and saved to documents folder.

    console.firebase.google.com/project/yourprojectname/settings/serviceaccounts/adminsdk

    Useful links:

    • Firebase Admin SDK Setup
    • Firebase Admin User Management
    • Firebase Admin Database API

    Then, play around using Windows cmd.exe or any other shell. The 'npm install -g' installs firebase-admin globally in your machine.

    $ npm install firebase-admin -g
    $ node
    > var admin = require("firebase-admin");
    > admin.initializeApp({
      credential: admin.credential.cert("./documents/yourprojectname-firebase-admin.json"),
      databaseURL: "https://yourprojectname.firebaseio.com"
    });
    > var db = admin.database();
    // Of course use an existent UID of your choice
    > admin.auth().getUser('2w8XEVe7qZaFn2ywc5MnlPdHN90s').then((user) => console.log
    (user))
    > admin.auth().deleteUser('2w8XEVe7qZaFn2ywc5MnlPdHN90s').then(function() {
        console.log("Successfully deleted user");
      }).catch(function(error) {
        console.log("Error deleting user:", error);
      });
    
    // To get access to some key/values in your Database:
    > var ref = db.ref("users/1234");
    > ref.once("value", function(snapshot) {
      console.log(snapshot.val());
    });
    

提交回复
热议问题