Firebase: Copying data from firebase database of one user to another users database

I have created an ionic app,which stores data which is specific to a user in firebase database ,now I want to share this data to another users account,with the email provided ,so that when he retrieves his firebase data,the data which I have shared is visible too…
for example if i have login in my account and stored name of five books and authors in firebase database.Now I want to share the names of 2 books and authors to another account ,by knowing their email address,i.e if now that users logs in and retrieves his list,the books and authors which I have shared is visible too on retrieval of items…

If this is my database structure,Anw!EMoih… and HvzQET6qBcR… are the Uid for the respective users…
Kq6v8w3aa2… and Kq6vNjd2sb… are different keys storing my data…
now I want this key should be copied to HvzQET6qBcR…,i.e for this user if I know this email…
Since the user uid is different for different users email…

1 Like

There are several ways to handle this kind of things, but I always like to optimize for reading, so here’s what I would do:

I would create a node called sharedLists, inside that node I would create a list of lists, like every node inside would be a shared list of books:

-sharedLists
  -s4x5ed67tv8
    -book1
    -book2
    -etc

Then I would store the ID of that shared list s4x5ed67tv8 inside the userProfile of every user who has access to that list, like:

-userProfile
  -user1ID
    -sharedLists:
      - s4x5ed67tv8: true

Then when querying, I can see which lists each user has access to and fetch those. And depending on the use-case, especially if there’s not that much data changing I would copy the entire list under the userProfile node, that way only one fetch brings me all the data I need, remember, text is cheap in the DB.

2 Likes

how can get userId ,i.e the the one to whom I am sharing my file,can we get user uid ,if we know the mail id of the authenticated user.

It would be best done server.side (or using Cloud Functions) so you can use the Firebase ADMIN SDK, there’s a method called getUserByEmail() that would do the trick.

can u please elaborate it a bit…please tell about this function getUserbyemail(),how can I use Firebase Admin SDk…

You can have the Admin guide here: https://firebase.google.com/docs/admin/setup

I am able to create a node which contains list of shared data…

now how I can implement this…

if i want to share this data to user with id HvzQEt6qBcRis…how should I provide access to user…

this.f = db.list('/sharedfiles');
share(name)
{
	this.f.push({
		name: name,
});

this.showToast(name+' added to shared list');
				}

on click of share file…

Do you have a database node for user profiles? Like to store information about users? I would store the ID of the shared list under that user profile.

so how should i store the id of shared list under that user profile…

Anw1EMoihGQkvUA4dJL9dsv1hZr1,

HvzQET6qBcRIs4s1n18oiHxxT8n2

are corresponding to a user profile…

how should I stored my shared files id say,Kq96RWxCAY8lA2hpNz8,if i want to share this data under this userprofile.,HvzQET6qBcRIs4s1n18oiHxxT8n2.,
please help me on this part…

Like I said, do you have a user profile node where you store the user’s information in the database? You can just pass it to that node, all you need is the ID, so you can have the profile be like this:

{
	userProfile: {
	  sharedLists: {
	     listId34edr56yghu8i: true,
	     listId34e567uyy8u84: true,
	     listId34edr56y31u82: true,
	  }
	}
}

ok…
so here is my users node with data stored corresponding to every user,now how can i sharefiles id to any user here,
basically how can i put the shared id:true here…

HvzQET6qBcRIs4s1n18oiHxxT8n2,oyW7rInaXfaw7fmTdaTgjtIOfn23 are corresponding users…with -KqDwsgOC_w7UDBKMEZs,etc as their data stored…

how to do this,how can i set shared :true corresponding to shared files id inside that users profile.

I am now able to add the shared file to a user id(for now I am adding it to my own user id,since I am not knowing the user id of another person).
I first moved the data to the shared files folder,and now I am just pushing to my user id,so its now visible to me,but here I can see same data aa with different key name…
Is it right?

I am facing issue in getting user uid from email… using this function getuserbyEmail(),
I installed the firebase admin…
.from npm install firebase-admin
I also downloaded the file service-accounts.json…from firebase account…

getuid()
{
	admin.auth().getUserByEmail('vgvithikagupta1@gmail.com')
  .then(function(userRecord) {
    // See the UserRecord reference doc for the contents of userRecord.
    console.log("Successfully fetched user data:", userRecord.toJSON());
  })
  .catch(function(error) {
    console.log("Error fetching user data:", error);
  });
}

Then I imported it …

import  * as admin from 'firebase-admin';
var serviceAccount = "../pages/serviceaccount.json";
admin.initializeApp({
 credential: admin.credential.cert(serviceAccount),
  databaseURL: "https://nimble-valve-148606.firebaseio.com"
});

but when I was running this ,I am getting error.
cannot find module Cannot find module ‘jsonwebtoken’. in Credential.js file inside firebase admin…

Please help me where I am wrong…

Sorry, I wasn’t clear enough, the admin SDK only works server side, so you’d need to handle that part through Firebase Cloud functions or creating a small server somewhere to do it.

You can’t use the admin SDK in the client.

Ok thanks can u please elaborate on how we can implement it using Firebase cloud Functions…

That’s a broad topic, I suggest you go through this first: https://javebratt.com/firebase-cloud-functions-profile/

2 Likes

Thanks …I think it will solve my issue…
Thanks again…:slight_smile:

1 Like

No problem, if you need anything else don’t hesitate to write :smile:

2 Likes

Sure… :-):slight_smile: