How to remove a Firebase user from an Android application?

I am trying to code the Delete User method in my Android application, but I am having problems every time I execute it. This method will be executed when the user clicks the "Delete account" button in the "Activities" section. My applications work with FirebaseUI Auth.

Here is the method:

private void deleteAccount() { Log.d(TAG, "ingreso a deleteAccount"); FirebaseAuth firebaseAuth = FirebaseAuth.getInstance(); final FirebaseUser currentUser = firebaseAuth.getCurrentUser(); currentUser.delete().addOnCompleteListener(new OnCompleteListener<Void>() { @Override public void onComplete(@NonNull Task<Void> task) { if (task.isSuccessful()) { Log.d(TAG,"OK! Works fine!"); startActivity(new Intent(Main3WelcomeActivity.this, Main3Activity.class)); finish(); } } }).addOnFailureListener(new OnFailureListener() { @Override public void onFailure(@NonNull Exception e) { Log.e(TAG,"Ocurrio un error durante la eliminación del usuario", e); } }); } 

1) When I perform this function, the message Smart Lock appears on the screen and the user logs in again. Here is a screenshot of this post.

Smartlock message

2) In other cases, when the user has logged in for a long time, the function throws an exception similar to the following:

 06-30 00:01:26.672 11152-11152/com.devpicon.android.firebasesamples E/Main3WelcomeActivity: Ocurrio un error durante la eliminación del usuario com.google.firebase.FirebaseException: An internal error has occured. [ CREDENTIAL_TOO_OLD_LOGIN_AGAIN ] at com.google.android.gms.internal.zzacq.zzbN(Unknown Source) at com.google.android.gms.internal.zzacn$zzg.zza(Unknown Source) at com.google.android.gms.internal.zzacy.zzbO(Unknown Source) at com.google.android.gms.internal.zzacy$zza.onFailure(Unknown Source) at com.google.android.gms.internal.zzact$zza.onTransact(Unknown Source) at android.os.Binder.execTransact(Binder.java:453) 

I read that I have to re-authenticate the user, but I'm not sure how to do this when I work with Google Sign In.

+13
source share
7 answers

According to the Firebase documentation, the user can delete() remove the user from Firebase

Before deleting a user, reAuthenticate the user.

Code example

  final FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser(); // Get auth credentials from the user for re-authentication. The example below shows // email and password credentials but there are multiple possible providers, // such as GoogleAuthProvider or FacebookAuthProvider. AuthCredential credential = EmailAuthProvider .getCredential(" user@example.com ", "password1234"); // Prompt the user to re-provide their sign-in credentials user.reauthenticate(credential) .addOnCompleteListener(new OnCompleteListener<Void>() { @Override public void onComplete(@NonNull Task<Void> task) { user.delete() .addOnCompleteListener(new OnCompleteListener<Void>() { @Override public void onComplete(@NonNull Task<Void> task) { if (task.isSuccessful()) { Log.d(TAG, "User account deleted."); } } }); } }); 

Details: https://firebase.google.com/docs/auth/android/manage-users#re-authenticate_a_user

If you want to re-authenticate with another singin provider, you need to change the Provider for GoogleAuthProvider below, this is sample code

 GoogleAuthProvider.getCredential(googleIdToken,null); 
+16
source

First of all, you need to save the authentication token or password at the time of user login. If your application does not provide services such as Google login, Facebook login or others, you just need to save the password.

 //If there 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(); } } }); } }); } 
+3
source

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

user.delete ()

in the onComplete () method, which always executes. Therefore, we need to add an if check to see if the task succeeds, as mentioned below.

 user.reauthenticate(credential) .addOnCompleteListener(new OnCompleteListener<Void>() { @Override public void onComplete(@NonNull Task<Void> task) { if (task.isSuccessful()) { Log.e("TAG", "onComplete: authentication complete"); user.delete() .addOnCompleteListener (new OnCompleteListener<Void>() { @Override public void onComplete(@NonNull Task<Void> 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(); } } }); 
+3
source

The delete callback is already handling the failure case, why are you adding addOnFailureListener later?

Try removing it as follows:

 private void deleteAccount() { Log.d(TAG, "ingreso a deleteAccount"); FirebaseAuth firebaseAuth = FirebaseAuth.getInstance(); final FirebaseUser currentUser = firebaseAuth.getCurrentUser(); currentUser.delete().addOnCompleteListener(new OnCompleteListener<Void>() { @Override public void onComplete(@NonNull Task<Void> task) { if (task.isSuccessful()) { Log.d(TAG,"OK! Works fine!"); startActivity(new Intent(Main3WelcomeActivity.this, Main3Activity.class)); finish(); } else { Log.w(TAG,"Something is wrong!"); } } }); } 
+2
source

Use the following methods: -

delete ()

equivalent to calling set(null) .

or

RemoveUser ()

 removeUser(credentials, [onComplete]) 
0
source

@ Android developers:

I ran into a problem when information about Firebase Auth was saved on the device disk AFTER the application was uninstalled. android:allowBackup="false" and after reading about it, I found that setting android:allowBackup="false" and android:fullBackupContent="false" in <application> Manifest <application> ensures that the identification information will not be saved after deletion applications.

Please note that this kind of persistence did not occur on all Android devices. In fact, this started to happen on one of my devices that never had this problem.

0
source

Only get the current user and delete it using the following method, it will work fine

 user.delete(); 

and you can add Oncompletelistner also with addind user.delete().addOnCompleteListner(new OnCompleteListner) and much more on

0
source

All Articles