【问题标题】:Creating multiple users in Firebase at the same time同时在 Firebase 中创建多个用户
【发布时间】:2020-08-15 17:00:14
【问题描述】:

嗯,不完全是同时。更像是一个接一个,在一个 for 循环中。

问题:所以在某些情况下,我使用的代码确实可以创建多个帐户,但它经常给我错误,即用户为空并且无法写入数据库。

代码:

Database db = FirebaseDatabase.getInstance().getReference();
String password = "1234567"; // Same password for all accounts
ArrayList emailArray = new ArrayList(); // For example, 20 premade emails

        for (int i = 0; i < emailArray.size(); i++){
            mAuth.createUserWithEmailAndPassword((String) emailArray.get(i), password).addOnCompleteListener(new OnCompleteListener<AuthResult>() {
                @Override
                public void onComplete(@NonNull Task<AuthResult> task) {
                    if (mAuth.getCurrentUser() != null) {
                        
                     String userId = mAuth.getCurrentUser().getUid();  // This value is returning null
                     
                     // Writing a bunch of things to the database for each user 
                     db.child(userId).child("name").setValue(...)
                     ...

                     mAuth.signOut();
                        
                    }
                }
            });
        }

我想我可能必须使用 AuthStateListener,但我不确定,也不知道如何实现它。我将如何实现这一目标?请让我知道我是否应该在我的帖子中包含其他任何内容。谢谢!

【问题讨论】:

    标签: java android firebase firebase-realtime-database firebase-authentication


    【解决方案1】:

    您不应该像这样使用客户端 Android SDK 创建多个用户。您的应用很有可能会被标记为滥用,并被系统锁定。

    对于管理用例,请在受信任的环境中使用 Admin SDK。


    在代码级别,出现问题是因为mAuth.getCurrentUser() 只有一个值,而您正尝试并行创建所有用户。您需要等待每个createUserWithEmailAndPassword 完成,然后再开始下一个。但如上所述:这种方法一开始就有缺陷,所以我不建议再走这条路。

    【讨论】:

    • 就我而言,我正在尝试创建多个帐户来测试各种回收站视图。我不想手动创建数百个帐户,而是想批量创建它们。这可以使用 Admin SDK 完成吗?
    • 是的。 Admin SDK 可以create users 获得更高的配额,或者您可以import users 而不会达到配额限制。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-08-07
    • 2018-06-23
    • 1970-01-01
    • 2021-08-04
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多