User Document ID is the same as User ID in Firestore

Alex Ali

I try to store users inside collection in Firestore as documents and want to make every User ID is same as Document ID.. I tried but it still gives me another ID for the user:

Future<void> userSetupDone() async {
CollectionReference users = FirebaseFirestore.instance.collection('Users');

final docUser = FirebaseFirestore.instance.collection('Users').doc();

FirebaseAuth auth = FirebaseAuth.instance;
String? uid = docUser.id;
// String? uid = auth.currentUser?.uid.toString();
String? email = auth.currentUser?.email.toString();
String? phone = auth.currentUser?.phoneNumber.toString();
String? displayName = auth.currentUser?.displayName.toString();

users.add({'Uid': uid, "Email": email, "Phone": phone, "Name": displayName});
return;
}

enter image description here

Vishal Thakkar

You have to update uid immediately after adding data to the database. Because you can not get id before adding that data from the Firestore Database.

You will get it by following code:

 Future<void> userSetupDone() async {
    CollectionReference users = FirebaseFirestore.instance.collection('Users');
    
    final docUser = FirebaseFirestore.instance.collection('Users').doc();
    
    FirebaseAuth auth = FirebaseAuth.instance;

    String? email = auth.currentUser?.email.toString();
    String? phone = auth.currentUser?.phoneNumber.toString();
    String? displayName = auth.currentUser?.displayName.toString();
    
    DocumentReference reference= await users.add({'Uid': '', "Email": email, "Phone": phone, "Name": displayName});
    await reference.update({"Uid": reference.id});
    return;
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Cloud FireStore multiple document with same iD which is the user UID

Firestore document id as user phone number - Flutter

For document id same as user uid, how to retrieve only one user's data from firestore?

How to create a Firebase Firestore document with user defined document ID?

Firestore What should I set the user's document id to?

in Swift, on Firestore, is it possible to query the user ID in 2 fields of each document?

How to add user UID in the place of firestore document ID

Is it ok to store a user id as the key of a field in a Firestore document?

Is my User ID supposed to be different than my Document ID for each User in my Users Collection (Firestore)?

user id not the same as user post id

Firestore Security Rules: Allow User To Create Doc Only If New Doc ID is same as User ID

Add data to firestore with User ID

How do I retrieve a document ID in Firestore after creating a user collection using Flutter

how to get data from cloud firestore where user.uid equal to document id in flutter?

Rule for Firestore that enforce user to write a document with id equals to its own uid

Firebase Firestore custom ID not creating new document after creating user properly

How to get current user's document id in Swift using Firebase Firestore?

Wait for user id when using firestore collections

How get user id in flutter firestore

How to search array and find user ID in Firestore

Sending and Retrieving user ID from Firebase Firestore

AngularFire retrieving document id base on user authentication

Not able to fetch particular user document by user id in flutter app

Save id to Firestore document

Codeigniter MySQL : Insert record not be same with user id

How to store the id of document inside same doc in flutter firestore

how to prevent access to other user with same user id (UID)

Ensure both user name and user id are the same in host and Docker container

how to add multiple id in mysql table with same user id?