簡體   English   中英

FireBase Auth createUserWithEmailAndPassword().then() 不起作用

[英]FireBase Auth createUserWithEmailAndPassword().then() not working

我正在開發一個涉及用戶 firebase 身份驗證的 android 應用程序以供用戶注冊。 注冊有效,但我想通過在.then() 中實現它來將用戶名添加到數據庫中,但我的 android 工作室不斷給出“未解決的方法 then(?)”。 我也在使用 catch,但這似乎工作正常。我正在編寫的代碼如下,其中 firebaseAuth 是 FirebaseAuth 類型的 object:

            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");
                }
        });

我在以下鏈接中發現了類似的問題,但即使嘗試了這個,它也不起作用。

我進一步查看了 firebase 文檔並找到了另一個在完整偵聽器上使用實現,但是本文檔中描述的錯誤代碼似乎不適用於它。

更新 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 文檔中給出的錯誤代碼的方法

我無法找到 .then() 問題的解決方案,但在對 getExceptions() 進行了更多搜索后,我在此鏈接上找到了解決方案。 現在我當前的實現如下所示:

            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找到了另一個替代實現。

我在文檔中為您找到了這個:

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

我在 (task.getException()) 的幫助下找到了我的問題,所以我希望它可能有助於解決您的問題。

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

               Log.d("---->",""+task.getException());
            }

           
        }
    });

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM