2

Is there a way to remove all of the users from my firebase simple login?

I'm using a firebase as a test db and part of my automated tests involve adding and removing users, at the beginning of my test I want to clear all of the users from the firebase so that the test always starts with empty state.

Aaron
  • 2,450
  • 1
  • 17
  • 23
  • http://stackoverflow.com/questions/38808712/delete-all-users-from-firebase-auth-console/42467103#42467103 – AAverin Feb 26 '17 at 10:03
  • You can use this: https://stackoverflow.com/a/56365233/8682568 This does not require the use of your browser's developers tools (which is unbearably slow). Rather, it uses the new Firebase Admin SDK to nuke all users quickly. – Sunit Gautam May 29 '19 at 17:11

1 Answers1

0

I quickly put this together and is untested. Mac/iOS code.

Note that Simple Login is depreciated so this assumes the current API is installed.

The idea here is that when a user is created in Firebase, it's also created in your app space, in a users node. This is a common technique in Firebase.

The user is stored in your app node as a key/value pair with the value being a dictionary of email and password.

When your app starts, it loads in all of the users in the app->users node and then iterates over those to get the users info (email & pw)

The user is removed from Firebase and then also removed from the users node.

        Firebase *usersNode = [mainAppNode childByAppendingPath:userNodeString];

        [usersNode observeSingleEventOfType:FEventTypeValue withBlock:^(FDataSnapshot *snapshot) {

            //iterate through the users loaded in via the snapshot
            for ( FDataSnapshot *child in snapshot.children) {

                NSDictionary *dict = child.value;
                NSString *uid = child.key; //need this to remove the user from the apps users node
                NSString *email = [dict objectForKey:@"email"];
                NSString *pw = [dict objectForKey:@"password"];

                //remove the firebase user
                [mainAppNode removeUser:email password:pw withCompletionBlock:^(NSError *error) {

                    if ( error ) {
                        NSLog(@"Could not delete user from firebase");
                    } else {

                       //get a ref to this users node
                       Firebase *userToRemoveRef = [usersNode childByAppendingPath:uid];
                       [userToRemoveRef removeValue]; //remove the user info from the app
                    }

                }];
            }

        }];
Jay
  • 34,438
  • 18
  • 52
  • 81
  • I defiantly create a users table and add the users email and firebase id but i don't store their password. So what you are saying is that I have to store their password in plain text in my firebase, then I can use these passwords to call remove user. I really appreciate the answer, and know that it would work, but is storing passes in plain text good practice? – Aaron Mar 27 '15 at 02:02
  • That is an excellent question. Firebase uses strong encryption to move data between client and server so I would think it's safe on that end. Also, putting proper rules in place would not allow anyone else, other than yourself to have access. Finally, encrypting the password stored in Firebase would make it really secure. We use RNCryptor to encrypt any sensitive data we store in firebase. https://github.com/RNCryptor/RNCryptor – Jay Mar 27 '15 at 10:56