Cloud Firestore associate logged user with collection - firebase

I'd like to know the best approach/pattern to implement for the following use case.
I'm using Firebase Auth to sign up/in users and it's working fine.
As soon as the auth is performed I need to fetch from a Cloud Firestore collection, say user_profile, the data related to the logged user.
The problem is that the collection has its id which is not related to the user, since it's automatically generated by Firestore. So I don't know what to put in <user>
db.collection("user_profile").doc(<user>).get()
A possible solution I came up with is to store the user id inside user_profile and then query the whole collection like this
db.collection("user_profile").where("id", "==", id_from_auth).get()
Making the id field index of the collection.
Is this a good approach?
Is there a better/smarter/automatic way of doing this?

It's customary to store per-user data in a document keyed by the uid of the user authenticated by Firebase Authentication.

Related

Is it possible for Firestore to generate the same uid when registering a user?

I have my backend hosted in firebase. Whenever a new user registers I create a new account in my firestore collection. Each user will be identified by a unique uid generated by firebase. Is it possible with enough users to generate the same uid and hence write over another user's data when registering? Or does firestore have any built-in measures to prevent this from happening? Thanks in advance.
Firebase Auth guarantees that each user will have a truly unique ID within your project. There is nothing you have to do. Just accept the UID it generates.

is there a way to update firestore document fields based on a firebase Auth user info?

I have successfully added some users' information in firestore documents.
And I have a flutter app where the Client can enter some information after registration, I want to use that information provided to match/link the authenticated user to firestore using a field in the user document, is that possible?
note:
- the data in firestore is created before the user registration. which means the registered user uid is different from the one created in firestore
I would appreciate help in this regard, what is the proper code to do so?
I'm pretty new to this.
user in firestore document id
user uid from firebase
you can create another collection and store other info with the document with documentId as same as uid.so that you can query the collection with the authenticated user with uid.

does signInAnonymously add a record to your Users collection?

I just ran a test of Auth.auth().signInAnonymously and printed the uid of the anonymously created User. I then looked up that ID in my Users collection and didn't find a record. Is that User document only created when you convert them to non-anonymous?
Thanks.
Is that User document only created when you convert them to non-anonymous?
No, the user will appear in the database only when you'll add it. Firebase authentication does not write any data to your Cloud Firestore database.

Storing Firebase UID in an external database

I am using firebase auth for a project. Since we don't want to be in charge of handling passwords, and eventually want to add social login we're going to use FireBase.
We have an .NET WEB API set up right now with its own database. In this database some tables have reference to user's. Example: Order table would need a customer id.
Since we're only using Firebase Auth we're thinking about storing some user data in our database. Specifically:
FirstName | LastName | Role.
Would it be okay to store the Firebase UID in our tables as well, or is the a different parameter we should be using?
Yes, what you want is a unique identifier for each user and sounds like Firebase's auth UID is unique across the same project. Based on Firebase's document:
getUid() Returns a string used to uniquely identify your user in
your Firebase project's user database.
Also check this answer:
Is Firebase UID unique across multiple apps?
Yes, it's very common to store Firebase Auth UIDs in a database to store per-user information.

Firebase auth, is the user uid security sensitive?

I was wondering if getAuth().uid is sensitive/private in anyway? I am planning to use it on a user post something like: post.created_by: getAuth().uid. This makes writing rules/logic a lot easier.
The other way is to use the push id when the user is added to the database, which I'm trying to avoid.
No its safe to use the uid and recommended, firebase uses auth to authenticate the user and the assign the uid to identify the user across firebase.
You will be using uid in your security rules and as well as to identify user info in your db records.
Manually creating another id will render the efficiency of firebase useless.

Resources