【问题标题】:Permission denied while configuring firestore in flutter在flutter中配置firestore时权限被拒绝
【发布时间】:2020-11-02 08:42:24
【问题描述】:

我正在尝试向我的应用添加安全规则,该应用具有 Firebase Firestore 功能,因此规则是

rules_version = '2';
service cloud.firestore {
  match /databases/{database}/documents {
    match /{document=**} {
      function isSignedIn() {
        return request.auth.uid != null;
      }
    
      allow read, write: if isSignedIn() && request.auth.uid == resource.data.uid
    }
  }
}

这是获取查询快照的功能

 Stream<QuerySnapshot>  diarySnapshot(String uid){

    return _firestore
        .collection(uid)
        .where('uid', isEqualTo:uid)
        .snapshots();

  }

但我遇到了错误

[Firestore]: Write failed at 2nVrS92SbOZocu2CJY3Tj68er1n2/rXZAWPxEqhtqwmOrtZfo: Status{code=PERMISSION_DENIED, description=Missing or insufficient permissions., cause=null}

还有一件事,每个用户都会在他们的 id 上拥有自己的收藏。

编辑

** 数据库服务.dart**

class DatabaseService{


  Firestore _firestore=Firestore.instance;




  /*Future<void> configure() async{
    final FirebaseApp app=await FirebaseApp.configure(name:'notes',
      options: FirebaseOptions(
          googleAppID: '1:1051484666895:android:7e879575351f8463b2f77a',
          projectID: 'notes-e2864',
          gcmSenderID:'1051484666895',
          apiKey: 'AIzaSyBrCie0qxxwn0KwSLJ1wwTxnVZ7nLz-QkY'

      ),

    );
    _firestore = Firestore(app: app);
  }

*/

  Future setNewData(String uid,String title,String diary,DateTime dateTime) async{

    await _firestore.collection(uid).document().setData({

      'title':title,
      'diary':diary,
      'dateTime':dateTime.toString()
    });
  }

  Stream<QuerySnapshot>  diarySnapshot(String uid){

    return _firestore
        .collection(uid)
        .where('uid', isEqualTo:uid)
        .snapshots();

  }


}

我尝试了注释和未注释的代码,注释首先被配置,然后程序运行。

ServiceViewModel

class ServiceViewModel extends ChangeNotifier {




  final AuthService _authService=AuthService();
  final DatabaseService _databaseService=DatabaseService();
  String _uid;

  /*ServiceViewModel(){
    _databaseService.configure();
  }*/



  void signInWithEmailAndPassword(String email,String password) async {
     await _authService.signInWithEmailAndPassword(email, password);

   }

  void registerInWithEmailAndPassword(String email,String password) async {
     await _authService.registerWithEmailAndPassword(email, password);
  }

  void signInWithGoogle()async {
   await _authService.signInWithGoogle();
    print('Sign In With Google');
   }

  void signOut(){
     _authService.signOut();
     _uid=null;
   }

  /* void setUid() async{
     _uid=await _authService.getCurrentUser();

   }*/

   void setData(String title, String diary, DateTime dateTime) async{
     await _authService.getCurrentUser().then((value) async {
       return await _databaseService.setNewData(value.toString(), title, diary, dateTime);
     });

   }

   Stream<QuerySnapshot> getSnapShot(String uid)   {
     return _databaseService.diarySnapshot(uid);
   }


}


【问题讨论】:

  • 您的代码中是否有任何地方对 Firestore 执行了写入或更新?
  • 请看这个我已经给出了所有的代码。

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


【解决方案1】:

resource.data 不存在于create,因此您出错了。

试试这个:

rules_version = '2';
service cloud.firestore {
  match /databases/{database}/documents {
    match /{document=**} {
      function isSignedIn() {
        return request.auth.uid != null;
      }
    
      allow create: if isSignedIn() && request.auth.uid == request.resource.data.uid;
      allow update: if isSignedIn() && request.auth.uid == resource.data.uid
        && request.auth.uid == request.resource.data.uid;
      allow read, delete: if isSignedIn() && request.auth.uid == resource.data.uid
    }
  }
}

编辑:更新不卡住的特殊情况

【讨论】:

  • 一个建议,请在每个文档中添加字段uid
猜你喜欢
  • 2019-03-20
  • 1970-01-01
  • 2018-06-11
  • 2021-11-16
  • 2021-10-31
  • 1970-01-01
  • 1970-01-01
  • 2020-10-25
  • 2019-02-24
相关资源
最近更新 更多