Firebase createUserWithEmailAndPassword task.isSuccessful()

时间:2019-04-01 17:19:35

标签: java android firebase firebase-authentication

我正在创建一个Android应用程序,并正在实现登录/注册功能。

我正处于注册活动成功在Firebase应用程序中创建用户条目的阶段,但是,我似乎无法跟踪任务是否成功。

Users being created

private void startRegister() {
    String email = mEmailField.getText().toString();
    String password = mPasswordField.getText().toString();
    String confirmPassword = mConfirmPassword.getText().toString();

    // Check that fields are not empty
    if (TextUtils.isEmpty(email) || TextUtils.isEmpty(password) || TextUtils.isEmpty(confirmPassword)) {

        Toast.makeText(Register.this, "Email, password or confirm password field cannot be empty.", Toast.LENGTH_LONG).show();
    } else if (!password.equals(confirmPassword)) {

        Toast.makeText(Register.this, "Password and confirm password should match", Toast.LENGTH_LONG).show();
    } else {

        mAuth.createUserWithEmailAndPassword(email, password).addOnSuccessListener(new OnSuccessListener<AuthResult>() {
            @Override
            public void onSuccess(AuthResult authResult) {

                Toast.makeText(Register.this, "Success", Toast.LENGTH_LONG).show();
            }
        }).addOnFailureListener(new OnFailureListener() {
            @Override
            public void onFailure(@NonNull Exception e) {

                Toast.makeText(Register.this, "Failure", Toast.LENGTH_LONG).show();
            }
        });
    }
}

if!task.isSuccessful()或else块都曾到达,但用户是在Firebase中创建的。为什么无法追踪成功/失败的任何想法?

对比:

这在我的登录课程中起作用。

        mAuth.signInWithEmailAndPassword(email, password).addOnCompleteListener(new OnCompleteListener<AuthResult>() {
            @Override
            public void onComplete(@NonNull Task<AuthResult> task) {

                if (!task.isSuccessful()) {
                    Toast.makeText(Login.this, "Credentials error, user may not exist.", Toast.LENGTH_LONG).show();
                }
            }
        });

2 个答案:

答案 0 :(得分:1)

很难说出当前的实现方式。 尝试直接添加onSuccess

mAuth.createUserWithEmailAndPassword(email, pass).addOnSuccessListener(new OnSuccessListener<AuthResult>() {
        @Override
        public void onSuccess(AuthResult authResult) {
           //done
        }
    }).addOnFailureListener(new OnFailureListener() {
        @Override
        public void onFailure(@NonNull Exception e) {
            //display toast if registering failed
            ToastRect.failed(RegisterActivity.this, getString(R.string.app_activities_error_text) 
        }
   });

答案 1 :(得分:0)

public class Register extends AppCompatActivity {

private EditText mEmailField;
private EditText mPasswordField;
private EditText mConfirmPassword;
private Button mRegisterButton;
private FirebaseAuth mAuth;
private FirebaseAuth.AuthStateListener mAuthListener;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    FirebaseApp.initializeApp(this);
    setContentView(R.layout.activity_register);

    mEmailField = findViewById(R.id.registerEmailField);
    mPasswordField = findViewById(R.id.registerPasswordField);
    mConfirmPassword = findViewById(R.id.registerConfirmPassword);
    mRegisterButton = findViewById(R.id.registerButton);
    mAuth = FirebaseAuth.getInstance();


    mAuthListener = new FirebaseAuth.AuthStateListener() {
        @Override
        public void onAuthStateChanged(@NonNull FirebaseAuth firebaseAuth) {

            if (firebaseAuth.getCurrentUser() != null) {
                 startActivity(new Intent(Register.this, UploadActivity.class));
            }
        }
    };

    // https://stackoverflow.com/questions/10936042/how-to-open-layout-on-button-click-android
    Button register = (Button) findViewById(R.id.navigate_to_login);
    register.setOnClickListener(new View.OnClickListener() {
        public void onClick(View view) {
            Intent myIntent = new Intent(view.getContext(), Login.class);
            startActivityForResult(myIntent, 0);
        }
    });

    mRegisterButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            startRegister();
        }
    });
}

@Override
protected void onStart() {
    super.onStart();

    mAuth.addAuthStateListener(mAuthListener);
}

private void startRegister() {
    String email = mEmailField.getText().toString();
    String password = mPasswordField.getText().toString();
    String confirmPassword = mConfirmPassword.getText().toString();

    // Check that fields are not empty
    if (TextUtils.isEmpty(email) || TextUtils.isEmpty(password) || TextUtils.isEmpty(confirmPassword)) {

        Toast.makeText(Register.this, "Email, password or confirm password field cannot be empty.", Toast.LENGTH_LONG).show();
    } else if (!password.equals(confirmPassword)) {

        Toast.makeText(Register.this, "Password and confirm password should match", Toast.LENGTH_LONG).show();
    } else {

        mAuth.createUserWithEmailAndPassword(email, password).addOnSuccessListener(new OnSuccessListener<AuthResult>() {
            @Override
            public void onSuccess(AuthResult authResult) {

                Toast.makeText(Register.this, "Success", Toast.LENGTH_LONG).show();
            }
        }).addOnFailureListener(new OnFailureListener() {
            @Override
            public void onFailure(@NonNull Exception e) {

                Toast.makeText(Register.this, "Failure", Toast.LENGTH_LONG).show();
            }
        });
    }
}

}

确认FirebaseAuth.AuthStateListener()正在启动。这是我的Login类中的错误复制和粘贴作业。这使我无法成功创建用户。

此修复程序如下所示:

public class Register extends AppCompatActivity {

private EditText mEmailField;
private EditText mPasswordField;
private EditText mConfirmPassword;
private Button mRegisterButton;
private FirebaseAuth mAuth;
private FirebaseAuth.AuthStateListener mAuthListener;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    FirebaseApp.initializeApp(this);
    setContentView(R.layout.activity_register);

    mEmailField = findViewById(R.id.registerEmailField);
    mPasswordField = findViewById(R.id.registerPasswordField);
    mConfirmPassword = findViewById(R.id.registerConfirmPassword);
    mRegisterButton = findViewById(R.id.registerButton);
    mAuth = FirebaseAuth.getInstance();


    // https://stackoverflow.com/questions/10936042/how-to-open-layout-on-button-click-android
    Button register = (Button) findViewById(R.id.navigate_to_login);
    register.setOnClickListener(new View.OnClickListener() {
        public void onClick(View view) {
            Intent myIntent = new Intent(view.getContext(), Login.class);
            startActivityForResult(myIntent, 0);
        }
    });

    mRegisterButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            startRegister();
        }
    });
}

@Override
protected void onStart() {
    super.onStart();
}

private void startRegister() {
    String email = mEmailField.getText().toString();
    String password = mPasswordField.getText().toString();
    String confirmPassword = mConfirmPassword.getText().toString();

    // Check that fields are not empty
    if (TextUtils.isEmpty(email) || TextUtils.isEmpty(password) || TextUtils.isEmpty(confirmPassword)) {

        Toast.makeText(Register.this, "Email, password or confirm password field cannot be empty.", Toast.LENGTH_LONG).show();
    } else if (!password.equals(confirmPassword)) {

        Toast.makeText(Register.this, "Password and confirm password should match", Toast.LENGTH_LONG).show();
    } else {

        mAuth.createUserWithEmailAndPassword(email, password).addOnCompleteListener(new OnCompleteListener<AuthResult>() {
            @Override
            public void onComplete(@NonNull Task<AuthResult> task) {
                if (task.isSuccessful()) {

                    Toast.makeText(Register.this, "Password and confirm password should match", Toast.LENGTH_LONG).show();
                }
            }
        });
    }
}

}