在Firebase中同时创建多个用户。

huangapple go评论54阅读模式
英文:

Creating multiple users in Firebase at the same time

问题

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

代码:

Database db = FirebaseDatabase.getInstance().getReference();
String password = "1234567"; // 所有帐户相同的密码
ArrayList emailArray = new ArrayList(); // 例如,20个预先创建的电子邮件

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();  // 此值返回为null
                
                // 为每个用户写入许多内容到数据库
                db.child(userId).child("name").setValue(...)
                ...
                
                mAuth.signOut();
                    
            }
        }
    });
}

我认为我可能需要使用一个 AuthStateListener,但我不确定我应该如何实现它。我该如何实现这个目标?请告诉我是否应该在我的帖子中包含其他任何内容。谢谢!

英文:

Well, not exactly at the same time. More like one after the other, in a for-loop.

Problem: So in some instances, the code I used did manage to create multiple accounts but it often gives me the error that the user is null and fails to write to the database.

The code:

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

        for (int i = 0; i &lt; emailArray.size(); i++){
            mAuth.createUserWithEmailAndPassword((String) emailArray.get(i), password).addOnCompleteListener(new OnCompleteListener&lt;AuthResult&gt;() {
                @Override
                public void onComplete(@NonNull Task&lt;AuthResult&gt; 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(&quot;name&quot;).setValue(...)
                     ...

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

I think I may have to use an AuthStateListener but I am unsure and I don't know how to implement it. How would I go about accomplishing this? Please let me know if I should include anything else in my post. Thank you!

答案1

得分: 2

你不应该使用客户端的Android SDK来创建多个用户,像这样操作会有很大的机会导致你的应用被标记为滥用行为,并被系统锁定。

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


从代码层面来看,问题出在mAuth.getCurrentUser()只有一个值,而你正尝试并行创建所有用户。你需要在开始下一个用户之前等待每个createUserWithEmailAndPassword完成。但如上所述:这种方法本身存在缺陷,因此我不建议继续追求这条路。

英文:

You should not use the client-side Android SDK to create multiple users like this. There's a significant chance that your app will be flagged for abuse, and locked out of the system.

For administrative use-cases, use the Admin SDK in a trusted environment.


On a code level, the problem occurs because there's only one value for mAuth.getCurrentUser() and you're trying to create all the users in parallel. You'll need to wait for each createUserWithEmailAndPassword to be completed, before starting on the next one. But as said above: this approach is flawed to begin with, so I don't recommend pursuing this path further.

huangapple
  • 本文由 发表于 2020年8月16日 01:00:14
  • 转载请务必保留本文链接:https://go.coder-hub.com/63428656.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定