Firebase create user without sign in

10,373

Here is a tested solution you can apply (just implemented a few minutes before).

For creating a new user account you need the reference of FirebaseAuth.

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();

       try { FirebaseApp myApp = FirebaseApp.initializeApp(getApplicationContext(), firebaseOptions, "AnyAppName");
        mAuth2 = FirebaseAuth.getInstance(myApp);
    } catch (IllegalStateException e){
        mAuth2 = FirebaseAuth.getInstance(FirebaseApp.getInstance("AnyAppName"));
    }

//..... 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();
                        }



                        // ...
                    }
                });

    }

The point where you have to worry(actually not):

The admin should only be able to create the new user accounts. But the above solutions is allowing all authenticated user to create a new user account.

So to solve this problem you can take help of your firebase real-time database. Just add a key like "is_user_admin" and set the value as true from the console itself. You just need to validate the user before someone is trying to create a new user account. And using this approach you can set your own admin.

As of now, I don't think there is firebase-admin SDK for android. So one can use the above approach.

Share:
10,373
RoRo88
Author by

RoRo88

Updated on June 14, 2022

Comments

  • RoRo88
    RoRo88 about 2 years

    I wish to create a new user account from my application when logged in as a "admin user", The issue is i just want to create it not actually sign in.

    is it possible to disable the automatic sign in when creating a new user with the email / password.

    I see others have asked this question in relation to JS and swift but can not seem to find any android specific related info. i'm trying to achieve the same thing as this person but with android

    any help appreciated

  • RoRo88
    RoRo88 over 7 years
    i appreciate you reply , but this does not solve my problem , this would log the user out of the system and they would need to log in again as a administrator, i wish to just stay logged in as the admin user and create the new AC, the admin user may be creating many accounts so logging out and back in after each on is not practical
  • Amit Upadhyay
    Amit Upadhyay over 7 years
    @RoRo88 : Updated my answer after testing it. Now this will solve your problem