'How serach users in firestore database (With Auth)?
I am new to the world of Firebase, I have been reviewing some things but I still have doubts about how to control users with auth and firestore.
I use auth to manage the user session, and firestore to store user information. In auth I use the typical email and password as a means of authentication.
How can I do to be able to search for the information of a specific user, if the person who wants to access him does not know his UID that auth assigns him?
My database structure in firestore is simple, the main users collection, inside it documents where the UID is the name.
firestore
.collection("user")
.doc('user.uid')
.set({
'username': username,
'email': email,
//Some fields here
})
Imagine that in flutter I have a TextFormField, in which user A wants to search for the username of B.
How can I accommodate my database so that I can look up the username and get their information? If the users do not know their UID.
Since to access the information, obviously I require the UID.
How can I do that?
I appreciate your answers, thank you very much.
Solution 1:[1]
If you're searching for a specific username from a collection, you can use where() and isEqualTo comparison operator.
// Let _username be the username that's being searched
firestore
.collection('user')
.where('username', isEqualTo: _username)
.get()
.then((QuerySnapshot snapshot){
// Handle the snapshot
});
More details can be read on the FlutterFire docs.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Omatt |
