0

In a project I'm working on, we are using firebase auth.

Inside the app, users are able to create accounts for other users. However, we have the problem that, whenever a new account is created, the new user is automatically signed in, so the session is lost, for example:

  • Person A creates an account, named A

  • A is automatically signed in (this is correct)

  • A creates an account for person B, named B

  • B is automatically signed in and A is kicked out (this is bad)

I found this answer to the same problem, but after trying the implementation, it still doesnt work (maybe the answer is outdated?). I am using firestore, not realtime, in case it is relevant.

This is my code:

FirebaseAuth mAuth;
FirebaseAuth mAuth2;
FirebaseFirestore firestore;

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_account_management);
    
    mAuth = FirebaseAuth.getInstance();
    firestore = FirebaseFirestore.getInstance();
    
    buildSecondAuth()
}

/*Builds the auxiliar mAuth in order to create a new user*/
private void buildSecondAuth() {
    FirebaseOptions firebaseOptions = new FirebaseOptions.Builder()
            .setDatabaseUrl(firestore.getApp().getOptions().getDatabaseUrl())
            .setApiKey(firestore.getApp().getOptions().getApiKey())
            .setApplicationId(firestore.getApp().getOptions().getProjectId()).build();
    try {
        FirebaseApp myApp = FirebaseApp.initializeApp(getApplicationContext(), firebaseOptions, mAuth.getApp().getName());
        mAuth2 = FirebaseAuth.getInstance(myApp);
    } catch (IllegalStateException e)
    {
        mAuth2 = FirebaseAuth.getInstance(FirebaseApp.getInstance(mAuth.getApp().getName()));
    }
}

/*Uses the auxiliar auth to create a new account*/
private void signUpNewUser()
{
    mAuthJugador.createUserWithEmailAndPassword(getEmail(), getPass())
            .addOnCompleteListener(this, new OnCompleteListener<AuthResult>()
            {
                @Override
                public void onComplete(@NonNull Task<AuthResult> task)
                {
                    if (task.isSuccessful())
                    {
                        mAuth2.signOut();
                    }
                    else
                    {
                        Log.w("TAG", "createUserWithEmail:failure", task.getException());
                    }
                }
            });

}
Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
Luis Fernandez
  • 539
  • 1
  • 4
  • 24

1 Answers1

0

I think you cannot create 2 instances of authencation for the same firebase app. You can create 2 instances for different firebase app in the same Android app.

For more info. See this previously answered post.

fatalcoder524
  • 1,428
  • 1
  • 7
  • 16