I have a Flutter app that is generating the following error that is displayed above the app bar
[cloud_firestore/permission-denied] The caller does not have
The error occurs when:
1). The current app user logs out of my app.
2). The login screen is presented
3). The user logs in with a different user email/password
The error occurs AFTER the user has logged in.
No error logs appear in the IDE
I have the following Firestore rule set up
rules_version = '2';
service cloud.firestore {
match /databases/{database}/documents/{collection} {
match /{document=**} {
allow read, write: if request.auth != null
}
}
}
Any suggestions?
2
Answers
Resolved. The problem was a RiverPod StreamProvider that was calling a Firebase stream, and the RiverPod provider was missing the 'autodispose' suffix.
Without the 'autodispose' suffix the Firebase stream with the old user credentials was kept alive for the new user.
Firestore security rules are not wildcard-matched by default. Accordingly, if you specify a rule for a certain
document
orcollection
, it wont necessarily matchdocuments
orsubcollections
with the same level but different names. For any singledocument
orcollection
you want to safeguard, rules must be explicitly defined.You must specifically set rules for each level if you want security rules to be applied recursively to all
documents
in acollection
orsubcollection
.Here is an example of a Security Rule:
You may refer to this documentation about Writing Rules in Cloud Firestore Security Rules and Structuring Cloud Firestore Security Rules.
You may also check this Stackoverflow answers that might help you.