I have problem with restructurizing my database.
So far I have different users storing measurement data. Keys are made of (userId+currentDate). But now I'd like to add sharing this data between users.
Users can send requests for permission to view each other data, but I don't really know where should I store this data.Should I add additional key(userId) with value (true) inside each node? Can I even update the database in such way - "If user1 is friend with user2, add user1:true to each of users2's nodes"?
How should I proceed?
You can create a second type of node, something like shared_with_me_<my_user_id> for the user requesting access to another user data. If agreed by him, you insert this user_id to that node (like <other_user_id>:true) so you can query it later.
That said, I really suggest that you start using the Cloud Firestore Database.
Related
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.
my Android app requirement is to assign old (already exists UID) UID to new users while signing in with Google so that the new user can access the old user data directly. Note: old user data is stored with UID as the node but in various Firebase Realtime Database top-level nodes. Is it possible to change the UID.
There is no way to assign a UID to a user that is created with a client-side SDK, as that would be a major security risk. After all: what is keeping other users then also claiming the UID of any other user in your app.
The typical way is to deal with a scenario like this is to either track who has access to whose data in the database, or to perform a data migration when the new user is created and you've validated that they're indeed supposed to "inherit" another user account's data.
You can use link method from firebase SDK. see here
My firebase database looks like below:
Suppose I have 50 users and I want to give access to node code-1 to user 1-20, code-2 to user 21-40 and code-3 to user 41-50. I have some problem and I don't want to use firebase authentication. Is there any way to do so? like sending data from user to firebase and then complete checking in Realtime database rules section and then grant permission.
Two options come to mind:
Security through obscurity: Rename code-1 to something unguessable like c17357345db9 and rename code-2 to 64af7b679319. Then share a link to an app that gives access to c17357345db9 to users 1-20, then share a different link for 64af7b679319 to users 21-40.
Create an API Server: Don't allow direct access to the RTDB but instead create a Cloud Function that acts as a REST API to retrieve the data based on the user's ID.
Neither are optimal and user 3 could easily share access with user 30 if they wanted to.
I am working on a chat app where messages store in firebase realtime database. Now if i make a node shown below ( chats-Between-A-and-B-Id are autogenerated chatroom keys ), so i want to know that when user S open chats with user T in chat app so Database will read only the messages stored in chats-Between-S-and-T-Id and will not read others chatroom messages !? am i right ? if yes then will it reduce the pricing?
chats-
chats-Between-A-and-B-Id-
messageID1-…
messageID2-…
messageID3-…
...
chats-Between-o-and-p-Id-…
chats-Between-s-and-t-Id-…
chats-Between-x-and-y-Id-…
OR
If i store data like shown below then if user S open chats of user T in chat app so Database will read whole messages and
cahts-
messageID1-…
messageID2-…
messageID3-…
...
sort them like (shown below) so it will read all messages and sort them and show in app to the user !? which will increase the pricing ?
if(chat.getReceiver() == senderID && chat.getSender() == receiverId ||
chat.getReceiver() == receiverId && chat.getSender() == senderID)
{
(mChatList as ArrayList<Chat>).add(chat) //show chats
}
I want to know which approach can save my money.
Edit
I am using the 2nd approach (i got it from a chat app tutorial). Where we sort messages according to senders and receiver but after reading the answer of #sharath i get that if i go with this it will read every message from chat node and then sort them according to sender-reciver and if there are 1 million messages from all the users then it will read 1 million messages and sort only some of them only for 2 users! It will make me poor. So i am thinking about using the 1st approach where i will get messages from chat>>roomIdBetween2Users so it will ont read others chat and give me secure, maintainable and affordable databse.
And this is the image of my database here chats is parent node contains all the messages of all the users and sort them as i mentioned in the code for 2 users. There are already approximately 500+ messages in chats. And it will be so dangerous in future.
Its a little tricky situation to answer this without knowing your firebase DB structure. Am making an assumption and generalising this answer.
Firebase is a No SQL Database which stores data in tree structure or Parent child manner
Main important logic you should think of while developing anything with NoSQL databases are you need to create the Firebase DatabaseReference to the deepest node possible. Firebase queries and reads data which comes under the referenced node.
For example : if you have firebase structure as below
See this image of Firebase sample structure
If you want to access chats of User S, Create Reference to ID_User_S node.
note: If you create reference top node "Chats" then snapshot returns all child information under it
Regarding pricing : Yes the lesser the data you read lesser the price you pay. If you are referencing to deepest node possible you read lesser data. Try to optimise all firebase queries to reach to deepest node possible for creating a reference.
Now coming to your structure
chats-
chats-Between-A-and-B-Id-
messageID1-…
messageID2-…
messageID3-…
...
chats-Between-o-and-p-Id-…
chats-Between-s-and-t-Id-…
chats-Between-x-and-y-Id-…
If you create a database reference to Chats node then each one trying to access the chat will read every child under it which includes others chats as well.
So for efficient usage create reference to chats-Between-A-and-B-Id- so that you can get exact list of messages under those 2 users
I have done phone authentication linked to my project but I am not pushing the data from android instead I have manually entered the user data in Firebase. And I want to retrieve the data from firebase using OTP verification and I just need to search the specific user data with phone number which I am entering while login. I want to show all fields to be displayed which are present in my firebase database into my android project.
I've tried many methods to receive the data but I am stuck... Please help me.
Database structure
Authentication panal
You need to logically bind your authenticated user and the data that you stored in your firebase. As per the image in your data base 'id' of the 'Users' are 1,2,.. like that. Instead of that you can use 'UID' of users as an an Id in your database. Go to your 'Authentication' section, you will see User UID against each registered user. Copy that and use as an ID while creating data in database. This will help you to get the data with reference like Users\. Hope this will solve your problem.