【问题标题】:Sign out error Cloud Firestore. The caller does not have permission to execute the specified operation注销错误 Cloud Firestore。调用者无权执行指定操作
【发布时间】:2021-03-03 10:48:35
【问题描述】:

我有一个 Flutter 应用程序,我已成功登录用户并且没有抛出任何错误。但是,当我在我的一个按钮中尝试 FirebaseAuth.instance.signOut() 时,它会注销用户但会引发此错误。我在下面附上了我的云存储规则。

rules_version = '2';
service cloud.firestore {
  match /databases/{database}/documents {
  
    match /merchants/{document=**} {
      allow read;
    }
    match /all_merchants/{document=**} {
      allow read;
    }
    match /Types/{document=**} {
      allow read;
    }
    match /Promotions/{document=**} {
      allow read;
    }
    match /search_types/{document=**} {
      allow read;
    }

    
    match /users/{userId} {
      allow read, update, delete: if request.auth.uid == userId;
      allow create: if request.auth.uid != null;
      
      match /{document=**}{
        allow read, update, create, delete: if request.auth.uid == userId;
      }
    }
    
     match /users/{email} {
      allow read, update, delete: if request.auth.token.email == email;
      allow create: if request.auth.token.email != null;
      
      match /{document=**}{
        allow read, update, create, delete: if request.auth.token.email == email;
      }
    }
    
    
  }
}

(编辑):我的按钮的 Flutter 代码如下:

import 'package:firebase_auth/firebase_auth.dart';
import 'package:flutter/material.dart';


class SignOut extends StatelessWidget {

  FirebaseAuth auth = FirebaseAuth.instance;


  @override
  Widget build(BuildContext context) {

GestureDetector(
      onTap: (){
          setState(() {
                auth.signOut();
               });
            },
      child: ListTile(
        dense: true,
        leading: Icon(Icons.exit_to_app, color: Colors.red[600], size: 25,),
        title: Text('Sign Out',
                    style: TextStyle(fontFamily: 'Lato', fontSize: 18, fontWeight: FontWeight.w500),),
        trailing: Icon(Icons.arrow_forward_ios, color: Colors.black, size: 19,),
      ),
    );
}
}

【问题讨论】:

  • 我认为该错误与安全规则无关。请编辑问题以显示未按预期方式运行的代码,以及导致错误的原因。
  • 已添加!谢谢

标签: firebase flutter dart google-cloud-firestore firebase-authentication


【解决方案1】:

发生这种情况是因为在您的应用中的某个地方,您可能仍在对某些在您退出后无法访问的 Firestore 查询进行流订阅,因为您使用规则正确地阻止了它。

检查您的项目代码并查找在用户退出之前未取消的任何最终订阅。

【讨论】:

  • 我的项目中有这个案例。我想知道是否有必要取消所有订阅,即使规则不再允许读取?在我的情况下,StreamBuilder 正在收听 Firestore 流(仅允许经过身份验证的用户读取)。所以在注销时,我正确地看到了这个错误。即使出现错误,流是否仍会尝试读取更改失败?订阅(由StreamBuilder 内部开始)是否应该在注销时取消?如果是这样,我们该怎么做?
  • 是的。 StreamBuilder 在一个自动处理的小部件中。如果您退出,您需要确保您没有依赖无效StreamBuilder 的活动小部件 - 这意味着您需要在退出之前处理这些小部件。
  • 是的,我在另一个地方也订阅了 Firestore 流(不是StreamBuilder)。但是我在注销前也是canceling 流,但我仍然在注销后立即看到Firestore 的permission-denied 错误。不知道为什么。
猜你喜欢
  • 2021-05-20
  • 2021-08-09
  • 2021-12-26
  • 2019-06-26
  • 2022-08-06
  • 2021-07-25
  • 2019-07-22
  • 2021-10-08
  • 1970-01-01
相关资源
最近更新 更多