I have a collection "user" and the document ID of the user is different from the current user’s UID. So when I use FirebaseAuth.instance.currentUser!.uid in the following code it doesn’t work. I am using anonymous login to create users not sure if this makes a difference.
FirebaseFirestore.instance.collection('user').doc(FirebaseAuth.instance.currentUser!.uid).collection('subcollection')
Document ID
UID
Would really appreciate it, if someone could clarify:
- If this is normal Firebase behavior make docid and uid different.
- How to access current user’s data in this scenario.
- How to make both the document ID and uid the same.
2
Answers
The following code creates the user document with UID as it's name.
The FirebaseAuth.instance.currentUser!.uid returns:
And to answer your questions.
No, it’s not. If you write a document to Firestore when the authentication is successful using the following document reference:
You’ll end up having a document in Firestore that has the same document ID as the UID that comes from the authentication process.
Exactly as already do, using
FirebaseAuth.instance.currentUser!.uid
.Using the above document reference and calling the DocumentReference#set() function, but only when the authentication is successful. Please also notice, that if you sign the user out and sign in anonymously again, a new
UID
is generated. There is no way you can use an old anonymousUID
.