Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reference Firebase user objects in Firestore

Is it possible to reference user objects in Firestore like a regular document from collection ? I've created a sample of fake users for test in the Authentication section, and I'd like to reference them in a collection.

Their is the reference field : enter image description here

The id seems correct, however calling a db.doc() with this reference as a parameter returns an empty document (snapshot.exists returns false).

The database 'users' isn't present in my collections since it is handled by Firestore authentication system, but I was wondering if their was a way to access it, similar to regular documents.

like image 686
KawaLo Avatar asked Nov 20 '19 22:11

KawaLo


People also ask

How do I reference someone on firestore?

Within Cloud Firestore there is no type that is a "reference to a Firebase Authentication user". But if you store user-specific documents in Firestore, you can use its Document Reference type to reference those user-specific documents. This user is first on the weekly Google Cloud leaderboard.

How do I store users in firestore database?

Save User Related DataOnce you have an authenticated user's unique identifier you can start to save documents in your Firestore with the UID as an identifier. Each time the user logs in you just look up the document in your Firestore to retrieve the related information about this user/login. });

Can you use Firebase and firestore together?

You can use both Firebase Realtime Database and Cloud Firestore in your app, and leverage each database solution's benefits to fit your needs. For example, you might want to leverage Realtime Database's support for presence, as outlined in Build Presence in Cloud Firestore.


1 Answers

You seem to be mixing up two products in the Firebase platform:

  • Firebase Authentication handles user sign-in.

    All information about these users is stored in an internal database, that you can only access through the Firebase Authentication APIs.

  • Cloud Firestore stores data that you put in it.

Firebase Authentication does not automatically create any user data in Firestore when a user is created. If you want such data in Firestore, you'll have to create it yourself, either in your application code, or in Cloud Functions in response to the user-created event.

Within Cloud Firestore there is no type that is a "reference to a Firebase Authentication user". But if you store user-specific documents in Firestore, you can use its Document Reference type to reference those user-specific documents.

like image 127
Frank van Puffelen Avatar answered Oct 01 '22 15:10

Frank van Puffelen