【问题标题】:In react native app (through Expo) using firestore permissions - request.auth is always null在使用 Firestore 权限的本机应用程序(通过 Expo)中 - request.auth 始终为空
【发布时间】:2018-05-31 06:02:42
【问题描述】:

这是我的代码:

firebase.auth().onAuthStateChanged((user) => {
    if (user) {
        console.log("We are authenticated now!");
        firebase.firestore().collection("users")
            .doc(firebase.auth().currentUser.uid).set({ name: "new name" });
    } else {
        loginWithFacebook();
    }
});

还有,这是我的许可规则:

service cloud.firestore {
  match /databases/{database}/documents {
    match /users/{userId} {
      allow read, write: if request.auth != null;
    }
  }
}

似乎无论如何,request.auth 总是为空。 我得到的错误:

Firestore (4.8.0) 2017-12-20T04:18:27.321Z [连接]:WebChannel 收到错误:{"code":403,"message":"缺失或不足 权限。","status":"PERMISSION_DENIED"}

【问题讨论】:

  • 你是如何测试这个的?您无法打印安全规则的值,那么您使用什么来验证它始终为空?您如何验证您正在连接到正确的端点?您在这里所拥有的一切看起来都不是问题,因此缺少一些东西。
  • @Kato - 简单:使用allow read, write: if true; 我可以读写数据库,而使用allow read, write: if request.auth != null; 我得到权限错误,这意味着 request.auth 为空。
  • 这并不意味着 request.auth 为空。可能只是您在协调身份验证之前尝试连接。
  • 我遇到了完全相同的问题。我正在尝试在验证后立即编写文档。我通过将规则设置为 'request.auth.userId != null' 验证了 id 是否为空,但它仍然失败。我还没有弄清楚什么是错的:(

标签: firebase react-native firebase-authentication google-cloud-firestore


【解决方案1】:

当我在 Expo 上使用最新的 Firebase JS SDK 时遇到此问题时,我已降级到 Firebase SDK 4.6.2。

此外,您必须这样做才能让一切正常工作:

复制https://gist.githubusercontent.com/mikelehen/444950a35019208f3aaf18d37ab4937c/raw/85ac4cb3108d92163cb8f04fbdddcc88d4081aab/index.js

通过您的node_modules/@firebase/webchannel-wrapper/dist/index.js

如果没有这个,您的收藏将一无所获。

希望这会有所帮助。

【讨论】:

  • 这应该被标记为这个问题的正确解决方案
  • 感谢您的解决方案。你有没有机会为此提交错误报告?
【解决方案2】:

我区分了来自在浏览器和 expo 中运行的相同代码的请求。我注意到的一个区别是缺少 Origin 标头。我修补了 xhr,它适用于我的用途。我不确定这是错误还是预期行为。我询问过 slack/firestore 和 discord/react-native,但没有得到太多意见。

const xhrInterceptor = require('react-native/Libraries/Network/XHRInterceptor')
xhrInterceptor.setSendCallback((data, xhr) => {
  if (xhr._method === 'POST') {
    // WHATWG specifies opaque origin as anything other than a uri tuple. It serializes to the string 'null'.
    // https://html.spec.whatwg.org/multipage/origin.html
    xhr.setRequestHeader('Origin', 'null')
  }
})
xhrInterceptor.enableInterception()

【讨论】:

  • 你是个传奇,杰德!谢谢!
【解决方案3】:

似乎是一个错误 - 其他知名用户也遇到过。当我们找到修复程序时,我会更新更多信息。如果有人让它工作(通过博览会反应原生),请分享。

【讨论】:

    【解决方案4】:

    对不起,我迟到了,但我刚刚遇到这个,想分享我的经验。我遇到了完全相同的问题,经过数月的研究和调试,当我升级到 firebase v 5 时,它自行解决了。这是我的 SO 帖子和解决方案的链接。希望能帮助到你: https://stackoverflow.com/a/51115385/6158840

    【讨论】:

      【解决方案5】:

      我通过将 Firebase 版本降级到 5.0.0(我使用的是 6.4 版)解决了这个问题。希望它可以帮助其他人..

      1. 在您的 package.json 中
      dependencies: {
        "firebase": "^5.0.0"
      }
      
      1. 更新依赖项

      npm install

      【讨论】:

        【解决方案6】:

        首先检查 FIREBSAE 规则执行模拟器上的东西,

        其次,您的问题听起来像是您的请求未同步, 在 Web 视图中检查您的规则(如果可能),因为我尝试这样做并且它对我有用

            {
              "rules": {
                        ".read": "auth != null",
                        ".write": "auth != null"
                       }
            }
        

        【讨论】:

        • 问题是关于 firestore 规则 - 它们与 firebase 的实时数据库不同。谢谢!
        【解决方案7】:

        请记录您的 UID 并确认它不为空。然后修改权限规则为 -

        // Grants a user access to a document matching their Auth user Id
            service cloud.firestore {
               match /databases/{database}/documents {
                 // Collection named "users", document named after the userId
               match /users/{userId} {
                 allow read, write: if request.auth.uid == userId;
               }
             }
          }
        

        【讨论】:

        • 谢谢 - 让我澄清一下:如果你仔细研究这个问题 - 你会发现只有在用户登录后才能完成所有操作(通过收听 onAuthChanged)。而且我的规则比你的限制更少——我检查的都是非空的——而且我一直都是假的。
        猜你喜欢
        • 2021-08-24
        • 2021-01-29
        • 2018-08-30
        • 1970-01-01
        • 1970-01-01
        • 2018-12-31
        • 2019-06-13
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多