Search code examples
javafirebasefirebase-securityfirebasesimpleloginfirebase-authentication

Firebase Authentication Limits


I am new to Firebase so any insights appreciated. I'm writing Java server side test code. I grab several users from an database and am trying to migrate the data into user authenticated nodes within Firebase. My code selects a few users from the DB and spins up a new thread for each user.

The first thread connects and authenticates successfully. Subsequent simultaneous authentication attempts fail with the error message below. Each thread has its own instance of a Firebase reference object. Is there a restriction on the number of simultaneous logins, perhaps from the same IP address? Haven't been able to find anything in the documentation yet.

If I change the code to run in a single thread and login and logout each user one by one then I do not receive an error.

Any insights much appreciated.

Message: -5
Message: Due to another authentication attempt, this authentication attempt was aborted before it could complete.

            Firebase ref = new Firebase("https://<instance>.firebaseio.com/");

            ref.authWithPassword(mEmail, mPassword, new Firebase.AuthResultHandler() {
            @Override
            public void onAuthenticated(AuthData authData) {
                System.out.println("Successfully authenticated: " + mEmail);
                user.setUID(authData.getUid());
                user.setCurrentUserRef(ref);
                done.set(true);
            }

            @Override
            public void onAuthenticationError(FirebaseError firebaseError) {
                System.out.println("Error during authentication: " + mEmail);
                System.out.println("Error during authentication: " + ref.toString());

                System.out.println("Message: " + firebaseError.getCode());
                System.out.println("Message: " + firebaseError.getDetails());
                System.out.println("Message: " + firebaseError.getMessage());

                done.set(true);
            }});
            waitForCompletion(this.getClass().getName());

Solution

  • If you are authenticating as different users because of security rules, using a server token is the better solution.

    A Firebase connection can only have at most one user authenticated at any given time. However in the Firebase Java library there is an undocumented (and not officially supported) workaround to create multiple independent connections. In a class that is in the package com.firebase.client you can run the following code

    // important this code needs to be in the package com.firebase.client
    Config config1 = new Config();
    Config config2 = new Config();
    Firebase ref1 = new Firebase("https://<your-firebase>.firebaseio.com", config1);
    Firebase ref2 = new Firebase("https://<your-firebase>.firebaseio.com", config2);
    // ref1 and ref2 will now have independent connections, listeners and authentication states
    

    Note that these will also open independent connections to the server.