使用Firebase身份验证,如何在不注销当前活动帐户的情况下创建帐户?

时间:2020-09-07 16:35:48

标签: java android firebase firebase-authentication

在我正在研究的项目中,我们正在使用Firebase身份验证。

在应用程序内部,用户可以为其他用户创建帐户。但是,我们有一个问题,每当创建一个新帐户时,新用户都会自动登录,因此会话丢失,例如:

  • 人员A创建一个名为A的帐户

  • A已自动登录(正确)

  • A为人B创建一个名为B的帐户

  • B自动登录,A被踢出(这很不好)

我找到了this的相同问题的答案,但是在尝试实施后,它仍然不起作用(也许答案已经过时了?)。如果相关,我正在使用Firestore,而不是实时的。

这是我的代码:

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

}

1 个答案:

答案 0 :(得分:0)

我认为您无法为同一Firebase应用创建2个身份验证实例。您可以在同一Android应用中为不同的Firebase应用创建2个实例。

有关更多信息。请参阅先前回答的post