Firebase user A transfer child content to user B. Possible? - android

Users gets to sign up/log in via email/password authentification on firebase. Done. I am also able to get the current user and also have him write personal info in his child also, game score points are updated to firebase as he progresses. Done.
Now, say I want user A to transfer points to user B. Knowing that user A is the current user. And perhaps user A knows user B's email or userID. How can I make him access/write in user B score points.
I have tried a few methods none worked:
I tried FirebaseDatabase.getInstance().get reference().child("profile/").child(X.getText().toString()).addListenerForSingleValueEvent blah blah blah.
Where X is B's user ID. Well this returned null.
I read about firebase-admin userRecord ..and after trying to put it in my Gradle and running into many errors I discovered it was for Server side. So not applicable.
I read about making a list of all users but dat didn't seem cool since what happens if I have a million users. It shows all when A only needs B's.
Oh. I'm a mechanical engineer. So please don't refer me to read firebase docs and etc . Most of the terms just confuses me and don't tag this as already asked question believe me I have read them all. Also, I'd very much love it if you could be kind enough to either refer me to a tutorial or explain the codes you have written using A and B as case study.

Here's an answer at kind of a high level:
I would suggest adding a node that will notify other users of pending transfers. Something like
transfers
transfer_0
fromUid: "uid_a"
toUid: "uid_b"
amt: "100"
transfer_1
fromUid: "uid_c"
toUid: "uid_d"
amt: "10"
All users should add an observer query to that node to watch for any newly added nodes where toUid equals their Uid. In this case childAdded for uid_b.
When user a transfers 100 to user b, it will be written to that node and user b will be notified the transfer node was added and can update their own node, then remove transfer_0.
Likewise, if they are offline and then log in, the childAdded will pull in any new transfers so then their node can be updated and the transfers removed when complete.

Related

How to give data access in cloud firestore to another added users in android?

I wanna ask about the concept and logically ways to give another user the privilege to access other's users' data. What I want to do exactly is like this :
Basically, collection 1 contains several Users ID (UID) from authentication, then the user will have their own data collected in collection 2 which contain the data ID.
So, it's like giving access to another user to collaborate with the data like Google Docs Apps where we can add another user to edit our documents. I've been thinking of how to do this, but still, I got stuck.
My question is, how can I possibly do this? cause from what I've read, cloud firestore don't use such a foreign key like MySQL. Thank You
haven't tried something like this but i think this approch overcomes your problem.
modify your structure according to above image. userID collection will contain userIds which are allowed to edit their parent collection.and create firestore rules according to your use to check weather the userId is allowed to edit the Collection or not.
in your case when 'user 2' will have reference to 'collection 2', he/she will try to change data. firebase rule will check if auth.userId is inside the 'collection2.UserIDs' or not and will allow according that.

User being faster than Cloud Function

I have a Cloud Function which is creating a user profile in Firestore, right after a user signs up with FirebaseAuth (with E-Mail & Password).
A user document could look like this
users/{<uid>}
{
username:"smith",
status: "I love pineapple pizza",
email:"smith#mail.com"
}
After signing up, I'm giving my users the chance to change their status attribute. The problem is that the user can be faster than the triggered cloud function. This leads to the problem that the user tries to change a document which is not existing.
How can I make sure that the cloud function is already triggered ?
The only thing which is on my mind is checking from the client side if the document exists (maybe a loop). Which is imo not a really good approach.
You could attach a listener to the document. With that your onSnapshot should fire when the document is created, at which point you can update it.
Depending on the exact writes the client and server need to do, also consider making the writes idempotent. So: ensure that whoever goes first, all writes succeed.
Checking to see if the document already exists is your only approach. You really don't have any guarantees about how exactly the client and server will interact with each other. If you need to perform some action on the client, it will have to coordinate with the server, with no assumptions about when exactly a Cloud Function may trigger.
If you want some client code to trigger when a function creates a document in Firestore, just set up a listener on that document (don't use get() - it should be a listener instead). The listener will get triggered when the document is created, and you will have assurance that the function is complete

Advice on direction for an Android App to receive data from other devices

For this question, I am not looking for specific answers, or code or anything, but moreso suggestions on what techniques to employ to accomplish my goal.
I am very new to Android development. I have a bit of a background in Java.
The App I'm looking to build would have two types of users. The Narrator and the Participant.
The Participants would fill out a few fields, and click a button. This data would be integer and string values. Nothing terribly complex. The participants don't really need any sort of feedback beyond success or failure.
The Narrator's app would receive this information, and build a list displaying the information the Participants sent. Possibly the Narrator is able to send a message to the Participant devices prompting them to fill out and send their inputs, but not necessary.
What I am looking for is direction on which sorts of methods or techniques to accomplish this. It could be accomplished over Bluetooth, but even accomplishing it over the internet is arguably better.
It would need to be able to generate a unique session that users can locate and "connect to", as well.
If there are other questions that have info that would help me, feel free to link them. I assume there are. The rub is that I wasn't exactly sure how to ask this question with enough brevity to yield useful search results.
Thanks very much.
Look at realtime cloud db solutions to create the realtime, connect to session that you want to create.
In your app create a registration activity where the user can register as a participant or narrator. Store the user details on the cloud once register with a usertype key under your user model/map.
For example to define a value to usertype use like this.
usertype = 0 for participant
usertype = 1 for narrator
For your main activity you can create 2 layouts:
Layout1 (participant)
Layout2(narrator)
When user login you read the usertype value and assign layout in the oncreate method.
Example
if(usertype==0){
setContentView(Layout1)}
Within the layouts you will add your edittexts, views and buttons as per required for narrators and participants.
You will also write your functions for narrator in its own class/void and the same for participant.
Then use the usertype value to access the defined classes/voids.
You can have a look at firebase as a realtime database or there are plenty other options.
Happy coding.
So according to your explanation, participants will send something to other users which are Narrator. So something like chat system. In that case, you can use Firebase Realtime database. First, see how Firebase real-time database works. You can check this to get an idea.
So when participants fill up something it must receive by the narrator. When participants submit something you just need to send a push notification to all participants. So when participants click on the notifications you will redirect him to a specific activity and can load the participant's submitted form in that.

How to get the number of users logged in in the firebase database (Without addValueEventListener() )?

I have been looking for this quite some time now and i haven't found any answer related to my problem, so before you tag my question duplicate, at least read it first.
First i have just started firebase and only know basic things about it, i am trying to make a simple single user (1 to 1) chat app.
I want to get the number of users logged in in the database.
I have a child to the root of my database called users which have the list of the users who have logged in.
I know about the datashot.getchildrencount() but that works when some update/event happens, but i want it to give me the number of users whenever i want, not only on some event (For example in messenger number of current active users are shown continuously,i dont want active user tho, i just want total logged in users).
I thought of the another way to make a child called NumberofUsers so i store number of users there but then firebase dont allow to getvalue of the child,only set value (it does allow to get value but only on some event). Any idea what should i do?
UPDATE
i thought of another way to do this, though its not working at the moment but i think solving it is easier than thinking of another method to solve my original problem.
so I made one more child of NumberofUsers with key "02" and random value. now everytime i want data of "01" i change the value "02" after enabling the addChildEventListener for "NumberofUsers". Code that does this. Error its giving me. .new database.
Apparently I cant access child data using datasnapshot, any solution?
Any idea how to solve either of this?
Thank you.
Unfortunately, there is no way in which you can read data from a Firebase Database without attaching a listener. Regarding Firebase, everything it's about listeners. In your case, i think you want a callback to be called once and then immediately removed. If so, i recomand you using addListenerForSingleValueEvent() method to simplify this scenario. It triggers once and then does not trigger again.
This is useful for data that only needs to be loaded once and isn't expected to change frequently or require active listening.
Hope it helps.

How would I go about creating a login

The set-up:
I have an android application that so far can register a user by inserting values into a remote mysql database. I'm now trying to implement the log in.
I was thinking that I can add a "logged in" column to the user table in the database that would store whether or not the user was logged in. Then I would have a trigger that would log the user off after a certain amount of time has been elapsed.
The application's use is to retrieve files based upon if the user has access to a certain file. For this I have an "access" column in the user table table specifying the access a user has to a certain file. I was thinking that when a user clicks an item in a list the application would send their login information and the server would determine if the information was correct then check to see if they had access to the specified file then send back the file if the information is correct.
The problem I'm having though is that checking the registration information takes about 2 seconds alone(due to connecting to the socket and sending a string over the network) and if I try to check both the login and the access id it would take slightly longer.
I feel as if I'm trying to reinvent the wheel but I can't find any viable resources on this matter. Criticisms? Suggestions?
(I wouldn't mind doing a complete redesign I just need to know where to start)
Never connect a client to a db-server. There's no way to intercept hacking attempts, because privileges are very basic (SELECT, UPDATE, etc., they ignore the query):
UPDATE users SET name='%s' WHERE userID=%i // where %i will be defined as the real userID
Above should be a valid query to update the user's account-information, however, a hacker can easily intercept this and change it into:
UPDATE users SET name='%s' WHERE userID=15 // ... or any other variable
Instead, you should create a web based API which will validate each query, or better, support only specific API-commands:
account/update.json?name=%s

Categories

Resources