Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to bulk delete Firebase anonymous users

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't actually want.

I can't see any way to bulk delete these users. Do I have to do it manually one-by-one? Is there anyway to use the API to access user accounts and manipulate them for users other than the current user?

like image 269
Matthew Gertner Avatar asked Sep 22 '16 13:09

Matthew Gertner


People also ask

How do I get a list of users from Firebase?

If you want to view a list of users that has registered thru Firebase Auth, you may view them in https://console.firebase.google.com/ then go to your project and select authentication , in the Users list is all the users that have registered thru Firebase Auth. Save this answer. Show activity on this post.

What is anonymous user in Firebase?

You can use Firebase Authentication to create and use temporary anonymous accounts to authenticate with Firebase. These temporary anonymous accounts can be used to allow users who haven't yet signed up to your app to work with data protected by security rules.


2 Answers

If you do not need to do it on a large scale and you want to delete some anonymous users from Firebase Console UI, but you are lazy to click on 250 users one-by-one, run the following code in your console (screen where table with users is shown):

rows = Array.from(document.querySelectorAll('td.auth-user-identifier-cell')).map(td => td.parentNode).filter((tr) => tr.innerText.includes('anonymous'))

var nextTick = null

function openContextMenu(tr) {
    console.log('openning menu')
    tr.querySelector('.edit-account-button').click()
    nextTick = deleteUser
}

function deleteUser() {
    console.log('deleting user')
    document.querySelector('.cdk-overlay-connected-position-bounding-box button:last-of-type').click()
    nextTick = confirmDelete
}

function confirmDelete() {
    console.log('confirming action')
    document.querySelector('.cdk-global-overlay-wrapper .confirm-button').click()
    nextTick = getUser
}

function getUser() {
    console.log('getting user')
    openContextMenu(rows.shift())
}

nextTick = getUser
step = 500
setInterval(() => {
    nextTick()
}, step)

It basically selects all rows which contain anonymous user and simulate you clicking the three dots, then clicking on delete account and as a last step it confirms action in the modal which appears.

Before running the script, select 250 rows per page in the table's footer. When all anonymous users are removed, you must manually go to next page and re run the script (or code in another "tick" which paginates for you).

It takes 1.5 second to delete one user (you can modify this with step variable, but I do not recommend go lower than 500ms - mind the UI animations).

It runs also in a tab in background so you can watch some YT in meantime :)

like image 200
miro Avatar answered Nov 01 '22 19:11

miro


Update 2021:

I had around 10,000 anonymous users, and @regretoverflow's solution lead to exceeding the delete user quota. However, slightly tweaking the code to utilize the admin's deleteUsers([userId1, userId2, ...]) API works like a charm.

function deleteAnonymousUsers(nextPageToken: string | undefined) {
  firebaseAdmin
    .auth()
    .listUsers(1000, nextPageToken)
    .then(function (listUsersResult) {
      const anonymousUsers: string[] = [];
      listUsersResult.users.forEach(function (userRecord) {
        if (userRecord.providerData.length === 0) {
          anonymousUsers.push(userRecord.uid);
        }
      });

      firebaseAdmin
        .auth()
        .deleteUsers(anonymousUsers)
        .then(function () {
          if (listUsersResult.pageToken) {
            // List next batch of users.
            deleteAnonymousUsers(listUsersResult.pageToken);
          }
        })
    })
}
deleteAnonymousUsers(undefined);
like image 37
John Connolly Avatar answered Nov 01 '22 19:11

John Connolly