身份验证失败FireBase Android

时间:2020-06-18 16:06:37

标签: android firebase firebase-authentication

我正在使用Firebase身份验证中的createAccount()方法,并且已附加了两个侦听器: addOnSuccessListeneraddOnFailureListener

  1. 在创建帐户并同时断开Internet连接时,该帐户已成功创建(我可以在Firebase控制台中看到它),而调用了onFailure侦听器。

  2. 此外,当我创建此帐户时,我将使用Task<Void>从auth方法异步调用的User对象保存在Firestore中。问题在于,由于先前的失败,从未在Firestore数据库中创建此用户,但无论如何都拥有身份验证帐户。

结果是用户可以登录,但不存在于数据库中,因此他的所有字段均为空。

我在Firebase身份验证方法上找不到任何回滚,所以现在我正在登录用户,这时检查数据库中是否存在具有相同电子邮件的User对象...但是我不知道认为这是一个好方法

那么如何处理这种情况呢?

编辑:我正在使用的代码

firebaseAPI.createAccount(email, password, userName).addOnSuccessListener(new OnSuccessListener<AuthResult>() {
    @Override
    public void onSuccess(AuthResult authResult) {
        System.out.println("onSuccess: the firebase user is not null.");
        final FirebaseUser currentUser = authResult.getUser();
        if (currentUser != null) {
            // Send an email to verify the new account created
            currentUser.sendEmailVerification()
                    .addOnSuccessListener(new OnSuccessListener<Void>() {
                        @Override
                        public void onSuccess(Void aVoid) {
                            System.out.println("onSuccess: email verification has been sent.");
                        }
                    })
                    .addOnFailureListener(new OnFailureListener() {
                        @Override
                        public void onFailure(@NonNull Exception e) {
                            System.out.println("onFailure: failed to send the emailVerification.");
                            System.out.println("onFailure: " + e.getMessage());
                        }
                    });

            System.out.println("onComplete: registering the username.");

            // Update the display name of the firebaseUser object
            final UserProfileChangeRequest profileUpdates = new UserProfileChangeRequest.Builder().setDisplayName(userName).build();

            currentUser.updateProfile(profileUpdates).addOnFailureListener(new OnFailureListener() {
                @Override
                public void onFailure(@NonNull Exception e) {
                    System.out.println("onFailure: " + e.getMessage());
                    System.out.println("onFailure: failed to update the user display name.");
                }
            }).addOnSuccessListener(new OnSuccessListener<Void>() {
                @Override
                public void onSuccess(Void aVoid) {
                    System.out.println("onSuccess: display name updated.");
                    System.out.println("onSuccess: writing batch.");
                    String uid = currentUser.getUid();
                    // Fields to register in Firestore Database.
                    final User userToRegister = new User(uid, userName, email, birthDate, lastName, firstName);
                    // Register the Username in Firestore Database & the user as the same time to rollback everything if something happens.
                    firebaseAPI.createUser(userToRegister, uid).addOnSuccessListener(new OnSuccessListener<Void>() {
                        @Override
                        public void onSuccess(Void aVoid) {
                            System.out.println("onSuccess: logging the user in.");
                            onSignUpSuccess(userToRegister);
                        }
                    }).addOnFailureListener(new OnFailureListener() {
                        @Override
                        public void onFailure(@NonNull Exception e) {
                            System.out.println("onFailure: " + e.getMessage());
                            System.out.println("onFailure: sending error to the activity");
                            if (e.getMessage().contains("offline") || e.getMessage().contains("internal error")) {
                                onSignUpError(SignUpErrorType.INTERNET_FAILED);
                            }
                            else {
                                onSignUpError(SignUpErrorType.GENERIC_ERROR);
                            }
                            deleteUser(currentUser);
                        }
                    }).addOnCanceledListener(new OnCanceledListener() {
                        @Override
                        public void onCanceled() {
                            System.out.println("onCanceled: batch canceled.");
                            onSignUpError(SignUpErrorType.GENERIC_ERROR);
                            deleteUser(currentUser);
                        }
                    });
                }
            });
        }
    }
}).addOnFailureListener(new OnFailureListener() {
    @Override
    public void onFailure(@NonNull Exception e) {
        System.out.println("onFailure: " + e.getMessage());
        if (e.getMessage().contains("internal error") || e.getMessage().contains("network error")) {
            // equals("An internal error has occurred. [ 7: ]")
            System.out.println("onFailure: sending error to the activity");
            onSignUpError(SignUpErrorType.INTERNET_FAILED);
        } else if (e.getMessage().contains("mail")){
            System.out.println("onFailure: sending error to the activity");
            System.out.println("onFailure: error is : " + e.getMessage());
            onSignUpError(SignUpErrorType.EMAIL_ALREADY_TAKEN);
        }
        else {
            System.out.println("onFailure: error is : " + e.getMessage());
            onSignUpError(SignUpErrorType.GENERIC_ERROR);
        }
    }
});

logcat错误:

I/System.out: onFailure: A network error (such as timeout, interrupted connection or unreachable host) has occurred.

0 个答案:

没有答案