0
votes

I have a working flutter app which uses the cloud_firestore package, it's going well with Android and iOS. But my database is "allow read,write: if true", and so Google keeps reminding me to fix this unsecurity. This I did:

  • including firebase_auth
  • changing my rules on firebase
  • creating a mail/password account
  • implementing the login procedure
  • changing the rules to "allow write: if request.auth != null;"

I think I made everything ok. In the app, it seems so:

print("User ${(await FirebaseAuth.instance.currentUser()).email})");

This gives me the mail of my account. Also I have a onAuthStageChanged listener and I get what I would expect. So I guess the login did work.

But if I try to make a database access e.g. with .setData(), I get an error

W/Firestore( 4411): (19.0.0) [Firestore]: Write failed at...: Status{code=PERMISSION_DENIED, description=Missing or insufficient permissions., cause=null}

It looked for me as if the auth package is not informing the firestore package correctly. In the logs, I see

D/FirebaseAuth( 5698): Notifying id token listeners about user (...).

I would expect something like "Firestore: received token...", but this does not come.

Do I have to be careful with the initialization order? Mine is

  1. _app = await Firebase.App.configure...
  2. _firestore = Firestore(app: _app);
  3. await FirebaseAuth.instance.signInWithEmailAndPassword...

I tried it on some Samsung and OnePlus devices with Android 8 and 9.

Any hints someone?

1
Can you mention how you fixed the firestore access? Have you added more rules to allow only allowed users? Those rules might be rejecting your requests? You might want to paste those rules to understand problem better.Chenna Reddy
Can you post some code please?Jairo R. Flores
these are my firebase rules. For testing, I switch between the "write" variants: service cloud.firestore { match /databases/{database}/documents { match /deviceinfos/{infos} { allow read: if true; // allow write: if true; allow write: if request.auth != null; } match /hardware/{hardware} { allow read: if true; allow write: if true; // allow write: if request.auth != null; } } }Jockel

1 Answers

0
votes

I got it running!! Used a hint from Flutter & Firebase - using firebase_storage along with firebase_auth

When accessing the database, I used a pointer to the firestore which I created like

static Firestore _firestore;
...
_firestore = Firestore(app: ...);

And later I used

await _firestore.collection(...)...setData(...);

But the Firestore seems to change after a login. It works when I write:

await Firestore.instance.collection...setData

Interesting!!