繁体   English   中英

未捕获(承诺)FirebaseError:缺少权限或权限不足

[英]Uncaught (in promise) FirebaseError: Missing or insufficient permissions

所以我在我的代码中运行条带支付方式,它会产生错误index.cjs.js:409 Uncaught (in promise) FirebaseError: Missing or insufficient permissions.

我已获得许可并将我的计划升级为 firebase 上的 blaze 计划以允许条带连接,但它仍然给我错误...有人有什么建议吗? 这是我的 firebase 规则:

rules_version = '2';
service cloud.firestore {
  match /databases/{database}/documents {
    match /environments/{environment}/companies/{userId} {
      allow write, read: if request.auth.uid == userId;
      match /{anySubCollection}/{anyDocument} {
        allow write, read: if request.auth.uid == userId;  
    
      }
    match /customers/{uid} {
      allow read: if request.auth.uid == uid;

      match /checkout_sessions/{id} {
        allow read, write: if request.auth.uid == uid;
      }
      match /subscriptions/{id} {
        allow read: if request.auth.uid == uid;
      }
    }

    match /products/{id} {
      allow read: if true;
      allow write: if false;

      match /prices/{id} {
        allow read: if true;
        allow write : if false;
      }

      match /tax_rates/{id} {
        allow read: if true;
      }
    
    }
 }}}

这是我尝试访问文档并在其中编写实际代码的代码:

 import firebase from 'firebase'
import getStripe from './stripe'

const firestore = firebase.firestore();

export async function createCheckoutSession(){
    // return firestore.collection()
    const checkoutSessionRef =  await firestore.collection('testdata').doc().collection('checkout_sessions').add(
        
        {price : price here
         success_url : window.location.origin,
         cancel_url: window.location.origin,
    }
    );

    checkoutSessionRef.onSnapshot(async (snap) => {
        const {sessionid} = snap.data();
        if (sessionid) {
             const stripe = await getStripe()
             stripe.redirectToCheckout({sessionid})
        }
    });
}

您的规则没有任何与名为“testdata”的集合匹配的条目。 如果没有匹配项,默认行为是拒绝访问,这就是您收到错误的原因。

我认为您的规则中可能有一些放错位置的大括号。 您已对其进行设置,以便所有内容都嵌套在match /environments/{environment}/companies/{userId} {的规则中。 我认为您的意思是在“客户”规则之前关闭该规则。

即使解决了这个问题,“testdata”仍然没有条目,所以你需要添加它。例如:

rules_version = '2';
service cloud.firestore {
  match /databases/{database}/documents {
    match /environments/{environment}/companies/{userId} {
      allow write, read: if request.auth.uid == userId;
      match /{anySubCollection}/{anyDocument} {
        allow write, read: if request.auth.uid == userId; 
      }
    }
      
    match /testdata/{uid} {
      allow read: if request.auth.uid == uid;

      match /checkout_sessions/{id} {
        allow read, write: if request.auth.uid == uid;
      }
    }

    // ..
  }
}

或者,也许您要访问的是“客户”集合。 在这种情况下,您不需要更改规则(除了右括号),您需要更改代码:

const checkoutSessionRef =  await firestore.collection('customers').doc().collection('checkout_sessions').add(

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM