You can store in your Customers lockers
the user code that entered the Qrcode and then when filtering its lokers
you bring only those who have the respective code.
When connecting to firebase you can authenticate by email/password, as follows
class Conection {
final FirebaseAuth _firebaseAuth = FirebaseAuth.instance;
Future<String> signInWithEmailAndPassword(String email, String password) async {
FirebaseUser user = await _firebaseAuth.signInWithEmailAndPassword(email: email, password: password);
user.email;
return user?.uid;
}
}
The method signInWithEmailAndPassword
will return the Document code created in your Firebase. Save this code to identify which user logged in.
When saving your new Qrcode, do it this way
await Firestore.instance
.collection("lockers")
.document()
.setData({"numero_serie": _numeroSerie, "userid": codigoSalvoAposConexao}); // se não existe grava no banco
So when you are going to filter the Qrcodes bring only those that have the userid
same as your currently logged in user.
final QuerySnapshot result = await Future.value(Firestore.instance
.collection("lockers")
.where("userid", isEqualTo: "$codigoSalvoAposConexao").where("numero_serie", isEqualTo: "$_numeroSerie")
.limit(1)
.getDocuments()); // verifica no banco se existe
Note: I don’t remember if this is the correct way to make a filter composed of two fields in firebase.
If you want to know a little more about logging in the way I mentioned, take a look here which is well explained. (It is in English but it is easy to understand)
Edited
To perform the filter with limit
that you want, I believe it is necessary to use the orderBy
before.
As I am without Flutter on my computer now, I can not be precise in the example, but try something like this:
final QuerySnapshot result = await Future.value(Firestore.instance
.collection("lockers")
.where("numero_serie", isEqualTo: "$_numeroSerie")
.where("userid", isEqualTo: "$codigoSalvoAposConexao")
.orderBy("userid")
.limit(1)
.getDocuments()); // verifica no banco se existe
Note: I believe the field used in orderBy
should be the same as where
preceding.
In your Heart
lockers
create a fielduserID
and save your logged-in user’s Document code on it. Then when you list, just search for the records that have theuserID
equal to the logged in user.– Matheus Ribeiro