How to delete a Firebase user from Android App?

前端 未结 8 1000
攒了一身酷
攒了一身酷 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条回答
  • 2020-12-14 09:08

    First of all, you need to store the auth token or the password at the moment your user is logging in. If your app doesn't provide such as Google Sign-in, Facebook Sign-in or others, you just need to store the password.

    //If there's any, delete all stored content from this user on Real Time Database. 
    yourDatabaseReferenceNode.removeValue();
    
    //Getting the user instance.
    final FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();
    
    if (user != null) {
        //You need to get here the token you saved at logging-in time.
        String token = "userSavedToken";
        //You need to get here the password you saved at logging-in time.
        String password = "userSavedPassword";
    
        AuthCredential credential;
    
        //This means you didn't have the token because user used like Facebook Sign-in method.
        if (token == null) {
           credential = EmailAuthProvider.getCredential(user.getEmail(), password);
        } else {
           //Doesn't matter if it was Facebook Sign-in or others. It will always work using GoogleAuthProvider for whatever the provider.
           credential = GoogleAuthProvider.getCredential(token, null);
        }
    
        //We have to reauthenticate user because we don't know how long 
        //it was the sign-in. Calling reauthenticate, will update the 
        //user login and prevent FirebaseException (CREDENTIAL_TOO_OLD_LOGIN_AGAIN) on user.delete()
        user.reauthenticate(credential)
                .addOnCompleteListener(new OnCompleteListener<Void>() {
                        @Override
                        public void onComplete(@NonNull Task<Void> task) {
                            //Calling delete to remove the user and wait for a result.
                            user.delete().addOnCompleteListener(new OnCompleteListener<Void>() {
                                @Override
                                public void onComplete(@NonNull Task<Void> task) {
                                    if (task.isSuccessful()) {
                                        //Ok, user remove
                                    } else {
                                        //Handle the exception
                                        task.getException();
                                    }
                                }
                            });
                        }
                    });    
    }        
    
    0 讨论(0)
  • 2020-12-14 09:11

    If you are using AuthUI or FirebaseAuth you can just do the following

     AuthUI.getInstance().delete(context).addOnSuccessListener {
               
     }.addOnFailureListener{
    
     }
    

    OR

    FirebaseAuth.getInstance().currentUser.delete().addOnSuccessListener...
    
    0 讨论(0)
提交回复
热议问题