在使用Android Studio中的Firebase时遇到的身份验证问题。

huangapple 未分类评论55阅读模式
英文:

authentication problem in fire base using android studio

问题

我正在进行一个项目,需要使用 Firebase 实用工具,所以我已经完成了所有必需的事情,但是我的身份验证没有进行,因此我的数据没有被上传到 Firestore 中。我尝试了很多方法,并发现在执行期间,我的 onComplete 监听器调用失败,因此会弹出一个消息框显示身份验证失败,所以我认为主要问题在于 onComplete 监听器,但我无法解决它。我的代码如下:

private TextView username;
private TextView password;
private AutoCompleteTextView email;
private ProgressBar progress_bar;
private FirebaseAuth firebaseAuth;
private FirebaseAuth.AuthStateListener authStateListener;
private FirebaseUser currentUser;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    username = findViewById(R.id.username_account);
    password = findViewById(R.id.password_account);
    email = findViewById(R.id.email_account);
    Button create_account = findViewById(R.id.create_acct_button);
    progress_bar = findViewById(R.id.create_acct_progress);
    firebaseAuth = FirebaseAuth.getInstance();

    create_account.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if (!TextUtils.isEmpty(email.getText().toString())
                    && !TextUtils.isEmpty(password.getText().toString())
                    && !TextUtils.isEmpty(username.getText().toString())) {
                String Email = email.getText().toString().trim();
                String Password = password.getText().toString().trim();
                String Username = username.getText().toString().trim();
                createUserEmailAccount(Email, Password, Username);
            } else {
                Toast.makeText(CreateAccountActivity.this,
                        "Empty Fields Not Allowed",
                        Toast.LENGTH_LONG)
                        .show();
            }
        }
    });
}

private void createUserEmailAccount(String email, String password, final String username) {
    if (!TextUtils.isEmpty(email) && !TextUtils.isEmpty(password) && !TextUtils.isEmpty(username)) {
        progress_bar.setVisibility((View.VISIBLE));

        firebaseAuth.createUserWithEmailAndPassword(email, password)
                .addOnCompleteListener(CreateAccountActivity.this, new OnCompleteListener<AuthResult>() {
                    @Override
                    public void onComplete(@NonNull Task<AuthResult> task) {
                        if (task.isSuccessful()) {
                            Map<String, Object> userobj = new HashMap<>();
                            userobj.put("userId", "currentuserId");
                            userobj.put("username", username);
                            db.collection("journal")
                                .add(userobj)
                                .addOnSuccessListener(new OnSuccessListener<DocumentReference>() {
                                    @Override
                                    public void onSuccess(DocumentReference documentReference) {
                                        Log.d(TAG, "DocumentSnapshot successfully written!");
                                        progress_bar.setVisibility(View.INVISIBLE);
                                    }
                                }).addOnFailureListener(new OnFailureListener() {
                                    @Override
                                    public void onFailure(@NonNull Exception e) {
                                        Log.w(TAG, "Error writing document", e);
                                    }
                                });
                        } else {
                            Log.w(TAG, "createUserWithEmail:failure", task.getException());
                            Toast.makeText(CreateAccountActivity.this, "Authentication failed.",
                                    Toast.LENGTH_SHORT).show();
                            progress_bar.setVisibility(View.INVISIBLE);
                        }
                    }
                });
    }
}

注意:这只是你提供的代码的翻译部分,可能需要检查和适应你的环境和需求。

英文:

I am working on a project which required firebase utilities so I did all those things required but my authentication is not taking place and thereby my data is not being uploaded in the firestore. I have tried many things and found that during the time of execution my onComplete listener is calling failure and thus a toast is popped authentication failure so I think the main problem lies in the onComplete listener but I couldn't fix it. My code is as follows-

*

private TextView username;
private TextView password;
private AutoCompleteTextView email;
private ProgressBar progress_bar;
private FirebaseAuth firebaseAuth;
private FirebaseAuth.AuthStateListener authStateListener;
private FirebaseUser currentUser;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    username = findViewById(R.id.username_account);
    password = findViewById(R.id.password_account);
    email = findViewById(R.id.email_account);
    Button create_account = findViewById(R.id.create_acct_button);
    progress_bar = findViewById(R.id.create_acct_progress);
    firebaseAuth = FirebaseAuth.getInstance();
    create_account.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if (!TextUtils.isEmpty(email.getText().toString())
                    &amp;&amp; !TextUtils.isEmpty(password.getText().toString())
                    &amp;&amp; !TextUtils.isEmpty(username.getText().toString())) {
                                    String Email = email.getText().toString().trim();
                String Password = password.getText().toString().trim();
                String Username = username.getText().toString().trim();
                createUserEmailAccount(Email, Password, Username);
            } else {
                Toast.makeText(CreateAccountActivity.this,
                        &quot;Empty Fields Not Allowed&quot;,
                        Toast.LENGTH_LONG)
                        .show();
            }
        }
    });
}
private void createUserEmailAccount(String email, String password,  final String username) {
    if (!TextUtils.isEmpty(email) &amp;&amp; !TextUtils.isEmpty(password) &amp;&amp; !TextUtils.isEmpty(username)) {
        progress_bar.setVisibility((View.VISIBLE));
    
        firebaseAuth.createUserWithEmailAndPassword(email, password)
                .addOnCompleteListener( CreateAccountActivity.this,new OnCompleteListener&lt;AuthResult&gt;() {
                    @Override
                    public void onComplete( @NonNull Task&lt;AuthResult&gt; task) {
                        
                        if (task.isSuccessful()) {
                           
                              Map&lt;String, Object&gt; userobj = new HashMap&lt;&gt;();
                              userobj.put(&quot;userId&quot;, &quot;currentuserId&quot;);
                              userobj.put(&quot;username&quot;, username);
                               db.collection(&quot;journal&quot;)
                                   .add(userobj)
                                    .addOnSuccessListener(new OnSuccessListener&lt;DocumentReference&gt;() {
                                     @Override
                                     public void onSuccess(DocumentReference documentReference) {
                                      
                                      Log.d(TAG, &quot;DocumentSnapshot successfully written!&quot;);
                                       progress_bar.setVisibility(View.INVISIBLE);
                                        }
                                      }).addOnFailureListener(new OnFailureListener() {
                                        @Override
                                     public void onFailure(@NonNull Exception e) {
                                   Log.w(TAG, &quot;Error writing document&quot;, e);
                    }
                });
            } else {
                            Log.w(TAG, &quot;createUserWithEmail:failure&quot;, task.getException());
                            Toast.makeText(CreateAccountActivity.this, &quot;Authentication failed.&quot;,
                                    Toast.LENGTH_SHORT).show();
                            progress_bar.setVisibility(View.INVISIBLE);
            }
            }
             });
            }
            
             }
         }*

huangapple
  • 本文由 发表于 2020年4月4日 03:34:38
  • 转载请务必保留本文链接:https://java.coder-hub.com/61019230.html
匿名

发表评论

匿名网友

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

确定