如何捕获Android Firebase signUpWithEmailAndPassword错误代码?

Bob*_*bby 4 android firebase firebase-authentication

我在线阅读JavaScript文档,您可以捕获从createUserWithEmailAndPassword函数返回的错误代码,以确定它是否已经使用过电子邮件,密码太弱等等.我如何在Java中执行此操作?

这在JavaScript中可以告诉它是什么错误.

firebase.auth().signInWithEmailAndPassword(email, password).catch(function(error) {
  // Handle Errors here.
  var errorCode = error.code;
  var errorMessage = error.message;
  // ...
});
Run Code Online (Sandbox Code Playgroud)

Sag*_*aut 5

看到此代码供您参考,这可能是您想要的确切答案,如果任务不成功则捕获异常,如下面的代码所示,

             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 not successful show error
                    if (!task.isSuccessful()) {
                        mAuthProgressDialog.dismiss();

                        try {
                            throw task.getException();
                        } catch (FirebaseAuthUserCollisionException e) {
                              // show error toast ot user ,user already exist

                            } catch (FirebaseNetworkException e) {
                            //show error tost network exception

                        } catch (Exception e) {
                            Log.e(LOG_TAG, e.getMessage());
                        }
                        Toast.makeText(CreateAccountActivity.this, R.string.log_error_occurred,
                                Toast.LENGTH_LONG).show();
                    } else {

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


                    }
                }

            });
Run Code Online (Sandbox Code Playgroud)