簡體   English   中英

AngularFirestore 權限缺失或不足(Firebase 自定義令牌身份驗證)

[英]AngularFirestore Missing or Insufficient permission (Firebase custom token auth)

我一直在嘗試從 Firestore 讀取數據,但無法這樣做......

async authUser() {
    let token = this._tokman.getUserToken();
    await this._afa.signInWithCustomToken(token);
  }

從服務器獲取令牌並將其傳遞給 firebase 以驗證用戶。 然后調用firestore獲取數據..

async data() {
    await this._afa.onAuthStateChanged(user => {
      if (user) {
        this._afs.collection('gps-data').doc(user.uid)
          .valueChanges()
          .subscribe(res => {
            console.log(res);
          }, err => {
            console.log(err);
          })
      }
    })
  }

但從firestore得到這個錯誤。

FirebaseError: Missing or insufficient permissions.
at new e (http://localhost:4200/vendor.js:51950:23)
at http://localhost:4200/vendor.js:62177:28
at http://localhost:4200/vendor.js:62178:18
at e.onMessage (http://localhost:4200/vendor.js:62200:10)
at http://localhost:4200/vendor.js:62117:26
at http://localhost:4200/vendor.js:62148:37
at http://localhost:4200/vendor.js:66953:31
at ZoneDelegate.invoke (http://localhost:4200/polyfills.js:460:30)
at Zone.run (http://localhost:4200/polyfills.js:219:47)
at http://localhost:4200/polyfills.js:953:40

Firestore 安全規則是

 service cloud.firestore {
  match /databases/{database}/documents {
    match /gps-data/{userid} {
        allow read: if request.auth.token.sub == userid;
    }
   
  }
}

還將 jwt 令牌添加到 http 標頭。 請指導我哪里做錯了。

更新解決方法:更改 firebase 版本

"firebase": "^7.0 || ^8.0"

"firebase": "^8.0.2"

不改變安全規則

問題是這一行中的token.sub

request.auth.token.sub == userid;

令牌的sub項是為其鑄造該令牌的項目 ID。 因此,您將項目 ID 與用戶 ID 進行比較,而兩者永遠不會相同。

您想在此處比較兩個用戶 ID,即:

request.auth.uid == userid;

我強烈建議花一些時間閱讀有關安全規則的文檔,特別是有關保護身份驗證僅允許內容所有者訪問的部分。

暫無
暫無

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

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