FireBase Auth createUserWithEmailAndPassword()。then()无法正常工作

时间:2020-05-03 12:44:56

标签: android firebase-authentication

我正在开发一个涉及用户Firebase身份验证以进行用户注册的android应用程序。注册工作正常,但是我想通过在.then()中实现用户名来将用户名添加到数据库中,但是我的android studio不断给出“ then(?)未解决的方法”。我也在使用catch,但似乎工作正常。我编写的代码如下,其中firebaseAuth是FirebaseAuth类型的对象:

            firebaseAuth.createUserWithEmailAndPassword(email,password)
                .then( (u) => {
                        //Implementation of then
                })
                .catch(error => {
                switch (error.code) {
                    case 'auth/email-already-in-use':
                        EmailWarning.setText("Email already in use");
                    case 'auth/invalid-email':
                        EmailWarning.setText("Invalid Email");
                    case 'auth/weak-password':
                        PasswordWarning.setText("Password should be 8 characters or longer");
                    default:
                        PasswordWarning.setText("Error during sign up");
                }
        });

我在遵循link时发现了类似的问题,但是即使尝试了此问题,也无法正常工作。

我进一步研究了Firebase文档,发现了另一个在完整侦听器here上使用的实现,但是此documentation中描述的错误代码似乎不适用于它。

更新1: 我最终在完整的侦听器上实现如下:

            firebaseAuth.createUserWithEmailAndPassword(email,password)
                .addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
                    @Override
                    public void onComplete(@NonNull Task<AuthResult> task) {
                        if (task.isSuccessful()) {
                            // Sign in success, update UI with the signed-in user's information
                            FirebaseUser user = firebaseAuth.getCurrentUser();
                            UserProfileChangeRequest profileUpdates = new UserProfileChangeRequest.Builder()
                                    .setDisplayName(Username)
                                    .build();

                            user.updateProfile(profileUpdates);
                        }
                        else {
                            // If sign up fails, display a message to the user.
                            switch (task.getException()) {
                                case "auth/email-already-in-use":
                                    EmailWarning.setText("Email already in use");
                                case "auth/invalid-email":
                                    EmailWarning.setText("Invalid Email");
                                case "auth/weak-password":
                                    PasswordWarning.setText("Password should be 8 characters or longer");
                                default:
                                    PasswordWarning.setText("Error during sign up");
                            }
                        }

                    }
                });

现在,用户添加部件的效果很好,但异常处理程序不适用于字符串,因此我找不到找到使用Firebase文档中给出的错误代码的方法

2 个答案:

答案 0 :(得分:2)

我无法找到.then()问题的解决方案,但是在深入研究getExceptions()之后,我在此link找到了解决方案。现在,我当前的实现如下:

            firebaseAuth.createUserWithEmailAndPassword(email,password)
                .addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
                    @Override
                    public void onComplete(@NonNull Task<AuthResult> task) {
                        if (task.isSuccessful()) {
                            // Sign in success, update UI with the signed-in user's information
                            FirebaseUser user = firebaseAuth.getCurrentUser();
                            UserProfileChangeRequest profileUpdates = new UserProfileChangeRequest.Builder()
                                    .setDisplayName(Username)
                                    .build();

                            user.updateProfile(profileUpdates);

                            OpenApp();
                        }
                        else {
                            // If sign up fails, display a message to the user.

                            String errorCode = ((FirebaseAuthException) task.getException()).getErrorCode();
                                if (task.getException() instanceof FirebaseAuthUserCollisionException)
                                    EmailWarning.setText("Email already in use");
                                else if(task.getException() instanceof FirebaseAuthInvalidCredentialsException)
                                    EmailWarning.setText("Invalid Email");
                                else if(task.getException() instanceof FirebaseAuthWeakPasswordException)
                                    PasswordWarning.setText("Password should be 8 characters or longer");
                                else
                                    PasswordWarning.setText("Error during sign up");
                            }
                        }
                });

还通过try catch和getException()。getErrorCode()here找到了另一种替代实现。

答案 1 :(得分:0)

我在文档中为您找到了这个

firebaseAuth.createUserWithEmailAndPassword(email, password)
    .addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
        @Override
        public void onComplete(@NonNull Task<AuthResult> task) {
            if (task.isSuccessful()) {
                // Sign in success, update UI with the signed-in user's information
                Log.d(TAG, "createUserWithEmail:success");
                FirebaseUser user = mAuth.getCurrentUser();
                updateUI(user);
            } else {
                // If sign in fails, display a message to the user.
                Log.w(TAG, "createUserWithEmail:failure", task.getException());
                Toast.makeText(EmailPasswordActivity.this, "Authentication failed.",
                        Toast.LENGTH_SHORT).show();
                updateUI(null);
            }

            // ...
        }
    });

您似乎需要使用.addOnCompleteListener

文档-> https://firebase.google.com/docs/auth/android/password-auth