Delete firebase user from database and authentication

Asked

Viewed 113 times

-1

Good afternoon I am trying to do the method that deletes the user from the database and authentication, but it is not working.

private void excluirDeslogar(){

        final DialogProgress dialogProgress = new DialogProgress();
        dialogProgress.show(getFragmentManager(), "3");
        
        String emailUsuarioLogado = firebaseAuth.getCurrentUser().getEmail();

         reference = ConfiguracaoFirebase.getFirebase();

     reference.child("usuarios").orderByChild("email").equalTo(emailUsuarioLogado).addValueEventListener(new ValueEventListener() {
          @Override
            public void onDataChange( @NonNull DataSnapshot snapshot) {

                for (DataSnapshot postSnapshot : snapshot.getChildren()) {

                    final Musico musico = postSnapshot.getValue(Musico.class);

                    FirebaseUser firebaseUser = FirebaseAuth.getInstance().getCurrentUser();
                    
                    //abaixo é o código que exclui do autenticação
                    firebaseUser.delete()
                            .addOnCompleteListener(new OnCompleteListener<Void>() {
                                @Override
                                public void onComplete(@NonNull Task<Void> task) {

                                    if (task.isSuccessful()) {
                                        dialogMensagem("Sucesso", "Sucesso ao EXCLUIR usuário");

                                    } else {

                                        dialogMensagem("Erro", "Erro ao EXCLUIR usuário");

                                        reference = ConfiguracaoFirebase.getFirebase();
                                        reference.child("Usuarios").child(musico.getuId()).removeValue();

                                        firebaseAuth.signOut();
                                        abrirTelaLogin();


                                    }

                                }
                            });
                }
            }


            @Override
            public void onCancelled(@NonNull DatabaseError error) {

            }
        });
        }




        private void abrirTelaLogin(){

            Intent intent = new Intent(getContext(), Login.class);

            startActivity(intent);
            finish();


        }

1 answer

0

I can’t believe this is gonna work. when using the currentUser.delete() method it depresses from firebase auth and thus does not need signOut manually. when undoing you lose the auth token and also it will no longer be possible to perform read/write in the firestore unless the rules are totally insecure.

The most suitable and safe method to perform such a task is to automate one in firebase functions and use firebase admin sdk to perform the task

exports.ondeleteuser = functions.auth.user().onDelete((user) => {
  // admin.firestore...
});

there is also an own extension that already perform this for you automatically without needing to write your cloud Function: https://firebase.google.com/products/extensions/delete-user-data?hl=pt-br

Browser other questions tagged

You are not signed in. Login or sign up in order to post.