简体   繁体   English

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

[英]How to catch Android Firebase signUpWithEmailAndPassword error code?

I see online in JavaScript documentation you can catch the error code returned from the createUserWithEmailAndPassword function to determine whether it's a email already used, password too weak etc. How do I do this in Java? 我在线阅读JavaScript文档,您可以捕获从createUserWithEmailAndPassword函数返回的错误代码,以确定它是否已经使用过电子邮件,密码太弱等等。如何在Java中执行此操作?

This in JavaScript can tell which error it is. 这在JavaScript中可以告诉它是什么错误。

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

see this code for your reference this might be the exact answer yo want, catch exception if task not successful as shown in below code, 看到此代码供您参考,这可能是您想要的确切答案,如果任务不成功则捕获异常,如下面的代码所示,

             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


                    }
                }

            });

I think task.getException() is the counterpart in Android for the errorCode you're looking for. 我认为task.getException()是您正在寻找的errorCode的 Android对应物。 The example in the docs shows it: 文档中的示例显示了:

Sign up new users 注册新用户

Create a new createAccount method which takes in an email address and password, validates them and then creates a new user with the createUserWithEmailAndPassword method. 创建一个新的createAccount方法,该方法接收电子邮件地址和密码,验证它们,然后使用createUserWithEmailAndPassword方法创建新用户。

 mAuth.createUserWithEmailAndPassword(email, password) .addOnCompleteListener(this, new OnCompleteListener<AuthResult>() { @Override public void onComplete(@NonNull Task<AuthResult> task) { Log.d(TAG, "createUserWithEmail:onComplete:" + task.isSuccessful()); // If sign in fails, display a message to the user. If sign in succeeds // the auth state listener will be notified and logic to handle the // signed in user can be handled in the listener. if (!task.isSuccessful()) { Toast.makeText(EmailPasswordActivity.this, R.string.auth_failed, Toast.LENGTH_SHORT).show(); } // ... } }); 

Add a form to register new users with their email and password and call this new method when it is submitted. 添加表单以使用其电子邮件和密码注册新用户,并在提交时调用此新方法。 You can see an example in our quickstart sample . 您可以在我们的快速入门示例中看到一个示例

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM