1

I want Admins to create users. But this will be on the client side as there will be many admins and I don't want that many people to have access to my firebase console. That's why I don't want to use Firebase-Admin SDK.

I have searched many places and found this. Below is that code:

So you can create two different FirebaseAuth objects like:

private FirebaseAuth mAuth1; private FirebaseAuth mAuth2;

Now in the onCreate you can initialize them as:

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);

        mAuth1 = FirebaseAuth.getInstance();

        FirebaseOptions firebaseOptions = new FirebaseOptions.Builder()
                .setDatabaseUrl("[Database_url_here]")
                .setApiKey("Web_API_KEY_HERE")
                .setApplicationId("PROJECT_ID_HERE").build();

        FirebaseApp myApp = FirebaseApp.initializeApp(getApplicationContext(),firebaseOptions,
                "AnyAppName");

        mAuth2 = FirebaseAuth.getInstance(myApp);
//..... other code here
}

To get ProjectID, WebAPI key you can go to Project Settings in your firebase project console.

Now to create the user account you have to use mAuth2, not mAuth1. And then on successful registration, you can log out that mAuth2 user.

Example:

private void createAccount(String email, String password)
    {
        mAuth2.createUserWithEmailAndPassword(email, password)
                .addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
                    @Override
                    public void onComplete(@NonNull Task<AuthResult> task) {


                        if (!task.isSuccessful()) {
                            String ex = task.getException().toString();
                            Toast.makeText(RegisterActivity.this, "Registration Failed"+ex,
                                    Toast.LENGTH_LONG).show();
                        }
                        else
                        {
                            Toast.makeText(RegisterActivity.this, "Registration successful",
                                    Toast.LENGTH_SHORT).show();
                            mAuth2.signOut();
                        }



                        // ...
                    }
                });

    }

But here I am able to create users but only once. After that I am not able to reopen that activity again. It is showing "FirebaseApp name AnyAppName already exists!".

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
Ashish Yadav
  • 543
  • 2
  • 7
  • 30
  • I think you should rephrase your question. Your current question would be answered with [my answer here](https://stackoverflow.com/questions/37517208/firebase-kicks-out-current-user/38013551#38013551). But in you last line you say you can actually do that but you have a different problem. – André Kool Apr 04 '18 at 11:11
  • Is this what you are looking for: [FirebaseApp name DEFAULT already exists](https://stackoverflow.com/questions/37787330/java-ee-firebaseapp-name-default-already-exists) – André Kool Apr 04 '18 at 11:16
  • Yes I saw your answer way before. But I wanted it in Java. In your answer also see Jimmy's comment. He had to add `secondaryApp.delete();`. But in Java what is the workaround? – Ashish Yadav Apr 04 '18 at 11:45
  • 2
    I dont think you have to delete it if i look at the other question i linked. Can you try to check to see if the app exists before you initialize it again in your oncreate? (something like [this answer](https://stackoverflow.com/a/47387163/4916627)) – André Kool Apr 04 '18 at 11:56
  • I am able to create as many users as I want until I stay in that activity. But as soon as go back and enter again the app crashes. Error in Log: `FirebaseApp name AnyAppName already exists!`. Thats why the `FirebaseApp` object `myApp` which was created is still there. I don't know if that makes any sense or not. I am new to Firebase. – Ashish Yadav Apr 04 '18 at 12:26
  • 2
    And that is exactly why you should check if it exists before you try to create it (again). If you don't know how you could check out [this comment](https://stackoverflow.com/questions/37634767/how-to-connect-to-more-than-one-firebase-database-from-an-android-app/37643374?noredirect=1#comment62899818_37643374) or maybe [this blog](https://sdgsystems.com/blog/firebase-connection-management) – André Kool Apr 04 '18 at 12:38

1 Answers1

1

Instead of initializing simply. Do this:

 try { FirebaseApp app = FirebaseApp.initializeApp(getApplicationContext(), firebaseOptions, "AnyAppName");
            mAuth2 = FirebaseAuth.getInstance(app);
        } catch (IllegalStateException e){
            mAuth2 = FirebaseAuth.getInstance(FirebaseApp.getInstance("AnyAppName"));
        }
Ashish Yadav
  • 543
  • 2
  • 7
  • 30