0

i am doing android studio project using Firebase,currently i had done with the registration and login,but my function is to transfer amount to another users with balance.

So what i need to do is to find the way to modify/update the balance content(value) after transferring the amount to another user.

but i do some researched they say Firebase only can only read/write with own user account.

assume i have already login to user1,and want to do the transfer to user2.

thanks.

-Users
      -AsjdnskkhdiioAndmmnekwas

         Name:"User1"

         email:"user1@hotmail.com"

         uid: "Adjshdkjwwnekwihwoi4kdnw4l2"

         age:20

         Balance:100

      -DdmkenklahaoinskaAnmdmls

         Name:"User2"

         email:"user2@hotmail.com"

         uid: "Cbdnaknekmmalsmdlen1qnio2"

         age:21

         Balance:100
-------------------------------------------------------------------
**how to become:**

 -Users
      -AsjdnskkhdiioAndmmnekwas

         Name:"User1"

         email:"user1@hotmail.com"

         uid: "Adjshdkjwwnekwihwoi4kdnw4l2"

         age:20

         Balance:50

      -DdmkenklahaoinskaAnmdmls

         Name:"User2"

         email:"user2@hotmail.com"

         uid: "Cbdnaknekmmalsmdlen1qnio2"

         age:21

         Balance:150
Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
albertto
  • 1
  • 1
  • 1
    You want to know how to use firebase api to update data in database? – TaQuangTu Jul 29 '19 at 16:46
  • @TaQuangTu yes,i am new to the firebase, and i followed the tutorial to create the register function and login function, but the problem is when i want to do transfer balance function i had stack,because i search over they have not related references for this, some of the post told that we only can read/write to own user account only. – albertto Jul 29 '19 at 16:50
  • You probably don't want one user to have permissions to change another users' record. Instead, you may want to consider saving the exchange to a trigger document and use a cloud function to update the user docs as needed. – Flignats Jul 29 '19 at 17:01
  • @sry,i am new to those firebase things,if using cloud function what should i focus on? or if i use mySQL(xampp local host) with update function(change the balance base on user id) will be better than this or not? because my project will only use on local such as PC and one phone device(hotspot with same environment between this 2 device ) Thanks. – albertto Jul 29 '19 at 17:09

2 Answers2

1

Remember that you are manager all information of all users (in your database). Use DatabaseReference setValue() method to update information inside database like this:

private DatabaseReference mDatabase;
mDatabase = FirebaseDatabase.getInstance().getReference();

String senderId = "sample_id_sender";
String receiverId = "sample_id_receiver";
mDatabase.child("users").child(senderId ).child("Balance").setValue(new_balance1);
mDatabase.child("users").child(receiverId).child("Balance").setValue(new_balance2);

How to know id of receiver??? it depends on your situation, example, you want to send 1$ to user whose name is "User2". You must find user2 id in list of users, in you database, I think these guide is good for you: https://firebase.google.com/docs/database/android/read-and-write https://firebase.google.com/docs/database/android/lists-of-data

TaQuangTu
  • 2,155
  • 2
  • 16
  • 30
0

Interesting problem. I think you can use a combination of a modified data model, a transaction and server-side security rules to implement this.

Step-wise:

  1. Since you'll need to know the current balance of both users in order to determine their new balance, you'll need to use a transaction.
  2. You'll need to store the actual transaction in some way, as otherwise the server won't be able to validate the write. The minimum info here is from UID, to UID and amount.
  3. In your security rules you should then modify that the total balance across the two accounts is unmodified across the transaction. For this you need the data from step 2, as you otherwise can't know the to account.
  4. Finally, in your security rules you'll want to validate that the only account whose balance can go down is the one of which the current user is the owner. In rules something like "balance": { ".validate": "data.child('uid').val() == auth.uid || newData.child('Balance').val() > data.child('Balance').val()" }

Scalability may be an issue though, as it's modifying nodes in multiple child nodes which means the transactions run across the entire top-level branch.

Also see:

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807