How to delete a Firebase user from Android App?

前端 未结 8 1095
攒了一身酷
攒了一身酷 2020-12-14 08:38

I\'m trying to code a Delete User method in my Android App, but I have some issues each time I execute it. This method will be executed when a user pushes the D

8条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-12-14 09:00

    The answer provided by Ansuita Jr. is very beautifully explained and is correct with just a small problem. The user gets deleted even without having successful re-authentication. This is because we use

    user.delete()

    in the onComplete() method which is always executed. Therefore, we need to add an if check to check whether the task is successful or not which is mentioned below

    user.reauthenticate(credential)
              .addOnCompleteListener(new OnCompleteListener() {
                 @Override
                 public void onComplete(@NonNull Task task) {
                     if (task.isSuccessful()) {
                        Log.e("TAG", "onComplete: authentication complete");
                        user.delete()
                        .addOnCompleteListener (new OnCompleteListener() {
                               @Override
                               public void onComplete(@NonNull Task task) {
                                    if (task.isSuccessful()) {
                                        Log.e("TAG", "User account deleted.");
                                    } else {
                                        Log.e("TAG", "User account deletion unsucessful.");
                                    }
                              }
                         });
                     } else {
                         Toast.makeText(UserProfileActivity.this, "Authentication failed", 
                                   Toast.LENGTH_SHORT).show();
                     }
                  }
             });
    

提交回复
热议问题