Permissions problems with Flutter and FireBase FireStore

huangapple go评论52阅读模式
英文:

Permissions problems with Flutter and FireBase FireStore

问题

抱歉,以下是翻译好的内容:

我是一个完全的 FireStore 和 FireBase 初学者。我试图做一些简单的事情,但 FireBase 告诉我我没有权限这样做。我正在使用 Flutter。在 FireBase 规则控制台中,我设置了以下规则。我只有一个项目和一个 Firestore 数据库。

rules_version = '2';
service cloud.firestore {
  match /databases/{database}/documents {
    match /{document=**} {
      allow write: if request.auth != null;
      allow read: if true;
    }
  }
}

我已经设置了一个用户集合,其中 uid 作为集合参数,并在匿名登录到 Flutter 后运行以下代码,不会引发错误。UserID 已设置为有效值。

void createUser(String userID, String userName, String password) async {

    final CollectionReference _users =
        FirebaseFirestore.instance.collection("Users");

    final data = {"Created": DateTime.now(), "LastLoggedIn": DateTime.now()};

    _users.doc(userID).set(data, SetOptions(merge: true));
}

我收到以下错误消息:

10.3.0 - [FirebaseFirestore][I-FST000001] 在 Users/QUIEvBpJeAgprgEan0S736aKjdk2 处写入失败:缺少或不足的权限。

我正在使用匿名登录。

即使我使用下面的规则,它被认为允许读写数据,我仍然收到相同的错误消息:

service cloud.firestore {
  match /databases/{database}/documents {
    match /{document=**} {
      allow read, write: if true;
    }
  }
}

我等待了几分钟以确保权限已设置并进行了测试。

英文:

I am a complete beginner to FireStore and FireBase. I am trying to do something simple and FireBase is saying I do not have permission to do it. I am using Flutter. I have the following rules set up in FireBase rules console. I only have one project and one Firestore database.

rules_version = '2';
service cloud.firestore {
  match /databases/{database}/documents {
    match /{document=**} {
      allow write: if request.auth != null;
      allow read: if true;
    }
  }
}

I have a collection of users setup which has a uid as the collection parameter and running the following code after signing into Flutter anonymously which does not throw an error. UserID is set to a valid value.

void createUser(String userID, String userName, String password) async {

    final CollectionReference _users =
        FirebaseFirestore.instance.collection("Users");

    final data = {"Created": DateTime.now(), "LastLoggedIn": DateTime.now()};

    _users.doc(userID).set(data, SetOptions(merge: true));
  }

I am getting the following error message:

> 10.3.0 - [FirebaseFirestore][I-FST000001] Write at Users/QUIEvBpJeAgprgEan0S736aKjdk2 failed: Missing or insufficient permissions.

I am using anonymous log-ons.

Even when I use the following rules which is supposed to allow all reading and writing of the data I get the same error:

  match /databases/{database}/documents {
    match /{document=**} {
      allow read, write: if true;
    }
  }
}

I allowed several minutes to go by between setting the permissions and testing.

答案1

得分: 1

你尝试过使用时间戳的默认规则吗:

rules_version = '2';
service cloud.firestore {
  match /databases/{database}/documents {
    match /{document=**} {
      allow read, write: if
          request.time < timestamp.date(2023, 11, 25);
    }
  }
}
英文:

Have you tried the default rule with timestamp :

rules_version = &#39;2&#39;;
service cloud.firestore {
  match /databases/{database}/documents {
    match /{document=**} {
      allow read, write: if
          request.time &lt; timestamp.date(2023, 11, 25);
    }
  }
}

huangapple
  • 本文由 发表于 2023年2月19日 09:27:12
  • 转载请务必保留本文链接:https://go.coder-hub.com/75497457.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定