Firebase安全规则 – 获取文档时出错:FirebaseError:缺少或权限不足

huangapple go评论62阅读模式
英文:

Firebase Security Rules - Error getting document: FirebaseError: Missing or insufficient permissions

问题

我正在编写一个使用 Firebase 认证通过 Google/Facebook 登录用户,然后在 Firestore 用户表中创建用户的 React 应用程序,根据以下代码:

function loginSocial(provider) {
    return auth.signInWithPopup(provider).then((res) => {

    // 我们已经登录,现在要检查数据库中是否存在文档
    
    console.log(user.uid, auth.currentUser.uid); 

    var docRef = app.firestore().collection('users', res.user.uid)

    docRef.get().then((doc) => {
    
      if (doc.exists) {
        // 如果文档存在,我们什么都不做
      } else {
        // 创建用户的代码
      }

问题在于 docRef.get() 调用失败了 - 从控制台中看到:

Uncaught (in promise) FirebaseError: Missing or insufficient permissions.

但是根据我的理解,我的 Firebase 规则应该允许这个操作通过:

rules_version = '2';

service cloud.firestore {
  
  match /databases/{database}/documents {
    match /users/{userId} {
      // 允许用户读取/更新自己的文档
      allow read, update: if request.auth != null && request.auth.uid == userId;
    }
    
    // 允许用户修改嵌套在自己文档下的集合
    match /users/{userId}/{document=**} {
      allow read, write: if request.auth != null && request.auth.uid == userId;
    }
  }
}

有什么原因会导致失败并且我可以做什么来解决这个问题?

注意这是我在控制台中看到的内容:

Firebase安全规则 – 获取文档时出错:FirebaseError:缺少或权限不足

英文:

I'm writing a react application that signs in a user via Google/Facebook using firebase auth and then looks to create a user in the users table if the user doc is not yet in the firestore users table. Per the following code:

function loginSocial(provider) {
    return auth.signInWithPopup(provider).then((res) => {

    //we've signed in and we now want to check if the doc exists in the db
    
    console.log(user.uid, auth.currentUser.uid); 

    var docRef = app.firestore().collection('users', res.user.uid)

    docRef.get().then((doc) => {
    
      if (doc.exists) {
        //if the doc exists then we don't do anything
      } else {
       //code to create user
      }

The issue is that the docRef.get() call is failing - from the console:

> Uncaught (in promise) FirebaseError: Missing or insufficient permissions.

But my firebase rules should be letting it through as I understand it:

rules_version = '2';

service cloud.firestore {
  
  match /databases/{database}/documents {
    match /users/{userId} {
      // allow users to read/update their own document
      allow read, update: if request.auth != null && request.auth.uid == userId;
    }
    
    // allow user to modify collections nested under their own document
    match /users/{userId}/{document=**} {
      allow read, write: if request.auth != null && request.auth.uid == userId;
    }
  }
}

Any ideas why this would be failing and what I can do instead?

Note this is what I see in the console:

Firebase安全规则 – 获取文档时出错:FirebaseError:缺少或权限不足

答案1

得分: 0

这是最终对我有效的代码,这可能是因为我使用了过时的 Firebase 版本:

var colRef = app.firestore().collection('users')

colRef.doc(user.uid).get().then((doc) => {
      if (doc.exists) {
        console.log("文档数据:", doc.data());
      }
      else{
        //创建用户
      }
    })
英文:

Here's the code that ended up working for me, this might be an issue of me using an outdated firebase version

var colRef = app.firestore().collection('users')

colRef.doc(user.uid).get().then((doc) => {
      if (doc.exists) {
        console.log("Document data:", doc.data());
      }
      else{
        //create user
      }
    })
  

huangapple
  • 本文由 发表于 2023年7月11日 05:28:51
  • 转载请务必保留本文链接:https://go.coder-hub.com/76657458.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定