为什么我无法创建firebase用户

时间:2017-06-03 14:06:45

标签: android android-studio firebase

我正在尝试创建一个firebase用户,但是我的代码失败了,继续收到身份验证失败的消息,你能告诉我为什么吗? 此外,我不知道新的firebase如何连接到我的帐户,我曾经创建一个Firebase对象并将我的链接传递给它,但现在不同了,我无法看到它在哪里(它要求登录我的webbrowser,就是这样,我在代码中找不到它的引用)

@Override
    public void onClick(View v) {
        pass.setText(pass.getText().toString().trim());
        email.setText(email.getText().toString().trim());
        mAuth.createUserWithEmailAndPassword(email.getText().toString(), pass.getText().toString())
                .addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
                    @Override
                    public void onComplete(@NonNull Task<AuthResult> task) {
                        if (task.isSuccessful()) {
                            FirebaseUser user = mAuth.getCurrentUser();
                        } else {
                            Toast.makeText(MainActivityC.this, "Authentication failed.",
                                    Toast.LENGTH_SHORT).show();
                        }
                    }
                });
    }

1 个答案:

答案 0 :(得分:2)

检查以下代码供您参考..它会对您有帮助。

    private FirebaseAuth mAuth;
    String mUserEmail = "sagar@example.com";
    String mPassword = "password";

  mAuth.createUserWithEmailAndPassword(mUserEmail, mPassword)
 .addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
            @Override
            public void onComplete(@NonNull Task<AuthResult> task) {

                Log.d(LOG_TAG, getString(R.string.log_message_auth_successful) + " createUserWithEmail:onComplete:" + task.isSuccessful());
             // if task is successful  then AuthStateListener  will get notified you can get user details there. 
                // if task is not successful show error
                if (!task.isSuccessful()) {

                    try {
                        throw task.getException();
                    } catch (FirebaseAuthUserCollisionException e) {
                        // log error here                            

                  } catch (FirebaseNetworkException e) {
                        // log error here  
                    } catch (Exception e) {
                     // log error here        
                     }

                    } else {

              // successfully user account created
             // now the AuthStateListener runs the onAuthStateChanged callback

                }
            }

        });
     }