How to make only the user who saved the String can see Flutter/Firebase?

Asked

Viewed 241 times

0

This is a code that saves a String qr code that does not repeat in the database, but this way everyone can access this data.  I already got the user ID but I still can’t filter to just see the list.

void SalvarMensagem() async {

  String uid = UserModel.of(context).firebaseUser.uid;

  this._status = _status;
  _numeroSerie = _status.substring(48, 80);

  final QuerySnapshot result = await Future.value(Firestore.instance
      .collection("lockers")
      .where("numero_serie", isEqualTo: "$_numeroSerie")
      .limit(1)
      .getDocuments());

  final List<DocumentSnapshot> documents = result.documents;
  if (documents.length == 1) {
   // duplicado(context);
  } else {
    await Firestore.instance
        .collection("lockers")
        .document()
        .setData({"numero_serie": _numeroSerie, "userId": uid});
  }
}

in the firebase bank: inserir a descrição da imagem aqui

  • 1

    In your Heart lockers create a field userID and save your logged-in user’s Document code on it. Then when you list, just search for the records that have the userID equal to the logged in user.

1 answer

1


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.

  • I edited my question photo, I was able to get the user code, but I’m thinking of a way to filter it

  • @Jacksonfelipemagnabosco the way I showed you in my answer, of using a where compound did not work?

  • it works but the limit is not working, it grass the same values in the bank, I’m researching some way limit(1) works

  • @Jacksonfelipemagnabosco added one more example in my reply, check it again and see if it solves your problem.

  • nothing, it just keeps working on the simple query, or it doesn’t save or save more than once the same string

  • @Jacksonfelipemagnabosco stick to Casesensitive in the name of the fields. Do a test, make sure when using the compound filter SEM the orderBy("userid").limit(1) is returned the correct documents.

Show 1 more comment

Browser other questions tagged

You are not signed in. Login or sign up in order to post.