我遇到了这个问题,并尝试使用我找到的所有解决方案进行修复,但仍然不起作用。我在 firebase cloud firestore 中的规则是:
service cloud.firestore {
match /databases/{database}/documents {
match /{document=**} {
allow read, write : if auth != null ;
}
}
}
而且我已经启用了匿名登录方法。
android/build.gradle
:
classpath 'com.android.tools.build:gradle:3.1.3'
classpath 'com.google.gms:google-services:4.0.1'
android/app/build.gradle
:
compile project(':react-native-firebase')
compile project(':react-native-fbsdk')
implementation project(':react-native-firebase')
implementation fileTree(dir: "libs", include: ["*.jar"])
implementation "com.android.support:appcompat-v7:${rootProject.ext.supportLibVersion}"
implementation "com.facebook.react:react-native:+" // From node_modules
implementation 'com.google.firebase:firebase-core:16.0.4'
implementation 'com.google.firebase:firebase-auth:16.0.4'
implementation 'com.google.firebase:firebase-firestore:17.1.0'
Testing.js
:
firebase.auth().signInAnonymously().then(()=>{
firebase.app().firestore().collection('Hello').doc('hello').set({
id:'fadsa'
}).catch((err)=>{
alert(err);
})
})
rules_version = '2';
service cloud.firestore {
match /databases/{database}/documents {
match /{document=**} {
allow read, write: if false;
}
}
}
改成这个
rules_version = '2';
service cloud.firestore {
match /databases/{database}/documents {
match /{document=**} {
allow read, write: if request.auth != null;
}
}
}
这是一个问题,因为您的数据库当前有规则。请检查两个数据库:Realtime 和 Firestore。
一般来说,拥有完整的安全规则或 RD 或 CF 逻辑不能完全理解的任何其他规则每次都会出现该错误。
> // Full security
>
> { "rules": {
> ".read": false,
> ".write": false } }
在 Firestore 中,您可以进行如下配置:
> service cloud.firestore { match /databases/{database}/documents {
> match /{document=**} {
> allow read: if auth != null;
> allow write: if auth != null;
> } } }
更多示例可以查看: https://gist.github.com/codediodeio/6dbce1305b9556c2136492522e2100f6 https://firebase.google.com/docs/database/security
如果您不在应用程序内使用身份验证而仅使用 Firebase Firestore。所以你可以像这样简单地改变它。
更改此
rules_version = '2';
service cloud.firestore {
match /databases/{database}/documents {
match /{document=**} {
allow read, write: if false;
}
}
}
进入此
rules_version = '2';
service cloud.firestore {
match /databases/{database}/documents {
match /{document=**} {
allow read, write: if true;
}
}
}
最后不要忘记发布这些更改以保存它。
如果您通过时间戳指定了
如果您通过时间戳指定了 Firestore 规则,则只需增加持续时间的长度即可。
rules_version = '2';
service cloud.firestore {
match /databases/{database}/documents {
match /{document=**} {
allow read, write: if
request.time < timestamp.date(2023, 3, 14);;
}
}
}
注意: 它仅用于练习和使用 Firestore 数据。如果您想构建生产级应用程序,请务必安全地定义您的 Firestore 规则。
只是想补充一点,如果您在 Firebase 中启用了“应用程序检查”并强制检查应用程序检查令牌,但请求中缺少令牌,也可能会发生此错误。
在这种情况下,您需要添加插件firebase_app_check。
我在 Android Stuido 上的 flutter 应用程序也遇到了同样的错误,当我检查规则时,我发现允许读写操作有时间限制。所以我只是延长了如下时间段:
rules_version = '2';
service cloud.firestore {
match /databases/{database}/documents {
match /{document=**} {
allow read, write: if
request.time < timestamp.date(2022, 9, 29);
}
}
}
只是指出,如果您有一个在您注销后运行的函数(可以是周期性的),也可能会发生这种情况。
我遇到了同样的错误,我挣扎了很多,因为我错过了“No App Check token for request”。警告。
如果您在应用程序检查中强制执行 Cloud Firestore,则必须配置、取消强制执行或添加令牌。 [Firebase 应用程序检查]
这个错误也发生在我身上,因为我正在使用内部测试轨道测试我的应用程序,并且我需要在 Google Play 控制台中为应用程序检查启用“测试 API 响应”。我还将我的应用程序的 SHA256 指纹从 Google Play 控制台添加到 Firebase 控制台。
你可以进入FirestoreDatabase,然后更新时间戳。让时间戳更长,然后单击“开发和测试”...
rules_version = '2';
服务cloud.firestore { 匹配 /databases/{database}/documents {
// This rule allows anyone with your Firestore database reference to view, edit,
// and delete all data in your Firestore database. It is useful for getting
// started, but it is configured to expire after 30 days because it
// leaves your app open to attackers. At that time, all client
// requests to your Firestore database will be denied.
//
// Make sure to write security rules for your app before that time, or else
// all client requests to your Firestore database will be denied until you Update
// your rules
match /{document=**} {
allow read, write: if request.time < timestamp.date(2025, 8, 9);
}
} }
在 Firestore 控制台上更新您的规则文件。 将读写权限设置为true。