【问题标题】:Firestore security rules Flutter app denied permissionsFirestore 安全规则 Flutter 应用拒绝权限
【发布时间】:2021-02-13 00:35:04
【问题描述】:

我目前正在尝试将我的 Firebase 后端连接到我的 Flutter 应用。在向 Firestore 创建一个简单的 get() 请求后,我每次都遇到相同的错误:

[cloud_firestore/permission-denied] The caller does not have permission to execute the specified operation.

我尝试过的安全规则:

service cloud.firestore {
  match /databases/{database}/documents {
    match /{document=**} {
      allow read, write: if request.time < timestamp.date(2020, 11, 30);
    }
  }
}
allow read, write;
allow read, write: if true;
allow read, write: if request.auth.uid != null;
allow read, write: if request.auth != null;
allow read, write: if auth != null ;

我确实在阳光下尝试了所有方法,但在读取或写入我的数据库方面没有成功,这让我相信问题出在我的代码中。我已检查以确保 Firebase 已正确初始化,Firebase Core 和 Firebase Firestore 也已正确初始化。

这是 Firebase 问题还是需要执行对 Firestore 的调用的特定方式?

获取请求:

  fetchChatMessages() async {
    var messageSnapShots =
        await FirebaseFirestore.instance.collection('topicofday').get();

    print('Messages: $messageSnapShots');
  }
}

初始化 Firebase:

class MyApp extends StatelessWidget {
  final Future<FirebaseApp> _initialization = Firebase.initializeApp();
  @override
  FutureBuilder build(BuildContext context) {
    return FutureBuilder(
      future: _initialization,
      builder: (BuildContext context, AsyncSnapshot<dynamic> snapshot) {
        if (snapshot.connectionState == ConnectionState.done) {
          return buildMaterialApp(context);
        }
        return Center(
          child: Container(
            child: CircularProgressIndicator(
              backgroundColor: Theme.of(context).primaryColor,
            ),
          ),
        );
      },
    );
  }

附加信息:

不确定这是否会影响请求,但我正在使用 Provider 进行状态管理。

【问题讨论】:

  • 您究竟在做什么来更新您的规则?编辑问题以完成您所采取的步骤。
  • 你指的是更新Firestore中的规则吗??就像对规则进行适当的更改然后发布一样?
  • 是的,您可能根本没有真正发布新规则,或者您可能正在其他项目中发布规则。
  • 为什么要设置这个条件?允许读、写: if request.time stackoverflow.com/questions/52422047/…
  • 你能分享你的解决方案作为这篇文章的答案吗?此信息可以帮助遇到类似情况的其他用户。

标签: flutter google-cloud-firestore firebase-security


【解决方案1】:

@DougStevenson 谢谢你!原来我试图从我的 Firebase 控制台中的错误项目中提取数据。奇怪的场景,但你的评论鼓励我跳出框框思考。

原因是在我的 Runner 文件中放置了来自不同项目的错误 GoogleService-Info.plist 文件。

【讨论】:

    猜你喜欢
    • 2020-03-22
    • 2016-11-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-03-20
    • 1970-01-01
    相关资源
    最近更新 更多