0

So i'm making an android application using Firebase where a trainer can manage their clients. When the client signs up they are prompted to enter the trainers ID so when the account is created their details get copied and stored under the trainers database like this:

Example trainer and client information

Basically the trainer should be able to delete clients from their database but is there a way to delete clients completely from application when logged in as a trainer? Or could you recommend a better way for trainers to manage clients than the way i have tried to implement?

Ezan
  • 43
  • 1
  • 8
  • So what is your goal? You want to delete `jDY1...D3R2` under `clients` node? – Alex Mamo Apr 21 '20 at 13:08
  • 1
    With the client-side Firebase Authentication SDK you'll only be able to delete the currently signed-in user. If they allowed anything else, that'd be a huge security risk. The option to delete other users *does* exist in the Firebase Admin SDKs, which are designed to run in a trusted environment, such as your development machine, a server that you control, or Cloud Functions. See https://stackoverflow.com/q/38800414, and https://stackoverflow.com/a/60974282 – Frank van Puffelen Apr 21 '20 at 13:11
  • @AlexMamo Yes under the clients node and delete the corresponding details in the authentication area of firebase – Ezan Apr 21 '20 at 13:14

1 Answers1

0

Basically it would be better to have a seperate node containing clients for each trainer, like this:

Clients

|
|-------trainerID
        |
         ____clientID1

         ____clientID2


Users
|
|--------trainerID
         |
          ----id:....
          ---image:...
          ----role:....

Using the above scheme, once you logged in as a trainer, try this:

//delete all clients
public void deleteClients(){

DatabaseReference mDatabase = FirebaseDatabase.getInstance().getReference();
mDatabase.child("Clients").child(trainerID).removeValue();
}

//delete specific client
public void deleteClient(String clientID){

DatabaseReference mDatabase = FirebaseDatabase.getInstance().getReference();
mDatabase.child("Clients").child(trainerID).child(clientID).removeValue();

}
Hasan Bou Taam
  • 4,017
  • 2
  • 12
  • 22