【问题标题】:firestore security rule request.auth.uid is not workingfirestore 安全规则 request.auth.uid 不起作用
【发布时间】:2018-05-28 06:26:08
【问题描述】:

Firestore 安全规则不起作用。 帮我。 无法读取用户/用户 ID 的文档数据。

---------安全规则------------

service cloud.firestore {
 match /databases/{database}/documents {
  match /users/{userId=**} {

  // Missing or insufficient permissions.
    allow read, write: if request.auth.uid == userId

  // this is work.
  //allow read, write: if request.auth != null

}

} }

-------------main.js--------

import Vue from 'vue'
import Quasar from 'quasar'
import firebase from 'firebase'
import 'firebase/firestore'

Vue.config.productionTip = false
Vue.use(Quasar)


let app;
firebase.initializeApp({
  apiKey: "",
  authDomain: "",
  databaseURL: "",
  projectId: "",
  storageBucket: "",
  messagingSenderId: ""
})


firebase.auth().onAuthStateChanged(user=> {
  if (user) {
    let ref = firebase.firestore().collection('users').doc(user.uid)
    ref.get().then(snapshot=>{
      // Error !! : Missing or insufficient permissions.
    }
  }
  if(!app){
    Quasar.start(() => {
      app = new Vue({
        el: '#q-app',
        render: h => h(require('./App').default)
      })
    })
  }

})

火力基地^4.8.0 Vue ^2.5.0

显然,require.auth.uid 似乎无法正常工作。 我哪里错了?

【问题讨论】:

  • 您是否将 auth.uid 密钥保存为用户密钥?
  • @ShinyaUeda - request.auth != null 不适合我。尽管已登录应用程序并在 Firebase Auth 控制台中看到用户 UID,但我在使用 com.google.firebase:firebase-firestore:17.1.0 时遇到了这个问题。
  • @Hareesh,如果request.auth != null 不起作用,有什么调试建议吗?

标签: firebase vue.js firebase-authentication firebase-security google-cloud-firestore


【解决方案1】:
 match /users/{userId}/{documents=**} {
  // return request.auth.uid == userId
  function isAuth(){
  return request.auth != null
  }
  allow read,write:if   (isAuth() && request.auth.uid == userId)    
}

此解决方案适用于此错误。

【讨论】:

  • 您的答案可以通过额外的支持信息得到改进。请edit 添加更多详细信息,例如引用或文档,以便其他人可以确认您的答案是正确的。你可以找到更多关于如何写好答案的信息in the help center
【解决方案2】:

我按照我找到的示例 here(在 用户 标签下)进行操作,效果很好:

// Grants a user access to a node matching their user ID
service firebase.storage {
  match /databases/{database}/documents {
    match /users/{userId}/{documents=**} {
      allow read, write: if isOwner(userId);
    }
  }

  function isOwner(userId) {
    return request.auth.uid == userId;
  }
}

【讨论】:

  • 效果很好,在我的情况下只需将 firebase.storage 更改为 cloud.firestore。谢谢!
【解决方案3】:

我自己解决了

 match /users/{user} {
   allow read, write: if request.auth.uid == user
   match / {docs = **} {
      allow read, write: if request.auth.uid == user
   }
 }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-01-31
    • 2018-03-19
    • 2021-05-12
    • 2021-11-22
    • 2019-09-06
    • 2020-08-24
    • 1970-01-01
    相关资源
    最近更新 更多