Reputation: 1
In my custom FlutterFlow Flutter function, I want to compare a Users document reference (userRef) to the user reference of the currently authenticated user. In the FlutterFlow builder, this comparison is easy to do, but within custom actions, I'm unsure how to retrieve the User reference for the authenticated user via code! Any guidance or tips would be greatly appreciated!
Here's a snippet of what I've tried:
if (userRef != authenticated.userRef) { // Perform some action... }
tldr: I've successfully obtained the userRef referencing a Users document, but I'm stuck on how to acquire the reference pointing to the authenticated user within a custom function.
Upvotes: 0
Views: 587
Reputation: 307
The Firebase Authentication App and Firestore App is separated. The usual practice is when a new user is created in Auth, we will use the auto-generated User UID from auth to create a new document in Firestore. This User UID will usually be saved as the document ID or as a field within the document.
So I believe what you need to do is to write a function to get the FirebaseAuth current user's UID and then query firestore for the document containing the UID. See example below where we assume you save the auto-generated UID as the document ID in 'Users' collection:
getUserDocRef() async {
String userUID = FirebaseAuth.instance.currentUser.uid;
DocumentSnapshot userSnapshot = await FirebaseFirestore.instance.collection('users').doc(userUID).get();
DocumentReference userRef = userSnapshot.reference;
}
Upvotes: 0