簡體   English   中英

Firebase 無法創建新文檔

[英]Firebase can't create new doc

我有一個 firebase 項目,自上次構建以來,該項目不會創建新文檔。 它將保存和修改文檔,但不會創建新文檔。

用於創建新文檔的 function 應該使用基於新創建的 uid 的密鑰創建一個新文檔,如下所示:

const sendInformationHandler = () => {
        const customerEmailCurrent = customerEmail.current.value
        const customerPassword = "password"
        if (checkUserEmailExists(customerEmailCurrent)) {
                createUser(
                    {
                        email: customerEmailCurrent,
                        password: customerPassword
                    }
                ).then(function(user) {
                    firebase
                    .firestore()
                    .collection('sessions')
                    .doc(user.data.uid).set(
                        {...context.state}
                    ).then(() => {
                        console.log("DATA SAVED SUCCESSFULLY!")
                    }).then(() => {
                        dispatch({ type: refreshContextFromCache, payload: INITIAL_CONTEXT})
                    }).then(() => {
                        push('/');
                    })
                }).catch(err => console.log("AN ERROR OCCURRED", err))
            } else {
                console.log("EMAIL ALREADY EXISTS!!!!!!")
            }
    };

我將 checkUserEmailExists 設置為始終返回 true。 createUser 是雲中的 firebase function 正在努力創建新用戶。 當觸發這個 function 時,它正在重定向,然后出錯。

該錯誤表明它是權限錯誤,但我的 firebase api 密鑰沒有更改,我可以修改現有記錄。

我的數據庫規則也沒有改變並且基於令牌,但令牌仍在工作:

rules_version = '2';
service cloud.firestore {
  match /databases/{database}/documents {
    match /{document=**} {
      allow read, write: if request.auth.token.admin == true;
      }
    match /{document=**} {
      allow create: if request.auth.token.coach == true;
      }
    match /sessions/{userId} {
      allow read, write: if request.auth.token.customer == true && request.auth.uid == userId;
      }
  }
}

在本地,在我推送之前,我在 firebase cli 中調用了 firebase 用於正確的項目,並在本地也使用 gcp 切換到正確的項目。

我的 firebaserc 是這樣的:

{
  "targets": {
    "prod": {
      "hosting": {
        "prod": [
          "prod-project"
        ]
      }
    },
    "develop": {
      "hosting": {
        "prod": [
          "prod-project"
        ]
      }
    },
    "prod-project": {
      "hosting": {
        "prod": [
          "prod-project"
        ]
      }
    },
    "ammonite-testing": {
      "hosting": {
        "prod": [
          "prod-project"
        ]
      }
    }
  },
  "projects": {
    "prod": "prod-project",
    "default": "prod-project"
  }
}

我的 firebase.json 是這樣的:

{
  "functions": {
    "source": "functions"
  },
  "hosting": {
    "target": "prod",
    "public": "build",
    "ignore": [
      "firebase.json",
      "**/.*",
      "**/node_modules/**"
    ],
    "rewrites": [
      {
        "source": "**",
        "destination": "/index.html"
      }
    ]
  }
}

到底是怎么回事? 如果 firebase 權限不起作用? 為什么我還能保存? 為什么它在這個最新的 ci 版本上壞了,而不是之前?

正如 Renaud Tarnec 提到的,您的規則相互重疊。 如 Firestore 文檔中所述,當多個規則與文檔匹配時,如果其中任何一個為真,則允許請求。 實際上,應用於sessions集合中文檔的規則是:

match /sessions/{userId} {
    allow read, write: if request.auth.token.admin == true or
                          request.auth.token.coach == true or
    }                     (request.auth.token.customer == true && request.auth.uid == userId)

至於為什么應用程序以前可以工作而現在不工作,我們無法給出答案。 可能是代碼的某些部分發生了變化並使錯誤浮出水面,這實際上意味着某些請求滿足上述安全標准之一而其他請求則不滿足。

如果您想在此級別進行調試,一個選項可能是將日志記錄代碼添加到此 function 和 Firebase function 以查看發送了哪些聲明(admin、coach、userId 等)。

另外,編寫 Firestore 安全規則時的最佳做法是完全避免規則重疊,並盡可能以最細粒度的級別授予訪問權限。 這有助於保護數據並確保根據需要限制訪問,意外的規則交互很容易被忽略。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM