首页 > 解决方案 > 使用firebase auth,如何在不登录当前活动帐户的情况下创建帐户?

问题描述

在我正在进行的一个项目中,我们使用的是 firebase auth。

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

我找到了同样问题的这个答案,但是在尝试实现之后,它仍然不起作用(也许答案已经过时了?)。我正在使用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());
                    }
                }
            });

}

标签: javaandroidfirebasefirebase-authentication

解决方案


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

了解更多信息。请参阅此先前已回答的帖子


推荐阅读