【问题标题】:Firebase database security rules to allow anonymous as well as web accessFirebase 数据库安全规则以允许匿名和 Web 访问
【发布时间】:2017-01-09 05:28:25
【问题描述】:

我的 firebase 数据库的以下规则允许通过我的 iOS 应用匿名登录。

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

这很好,但现在我需要为我的 JS Web 应用程序添加访问权限,该应用程序通过 apiKey 工作。如何在此处添加该规则?

【问题讨论】:

    标签: javascript firebase firebase-realtime-database firebase-security


    【解决方案1】:

    你能在 JS 网页应用上匿名登录吗?我从https://firebase.google.com/docs/auth/web/anonymous-auth复制了这段代码

    firebase.auth().signInAnonymously().catch(function(error) {
      // Handle Errors here.
      var errorCode = error.code;
      var errorMessage = error.message;
      // ...
    });
    

    如果您不想登录(我认为您不想登录),只需替换

    auth != null
    

    true
    

    【讨论】:

    • 如果我们将 auth != null 替换为 true,那么匿名登录的意义何在?此外,API 将被公开。例如,在我的应用程序中,我想要匿名登录,因为在用户登录之前获取了一些 API。为此,我使用从匿名登录生成的 tokenId 并将其传递到我的 API 调用的末尾。但是,当我尝试运行代码时,它显示 401 unauthorized。知道我们如何实现这一目标吗?
    猜你喜欢
    • 1970-01-01
    • 2018-12-15
    • 2016-09-28
    • 1970-01-01
    • 2020-07-11
    • 2020-10-19
    • 2018-10-22
    • 2021-01-02
    • 2023-03-13
    相关资源
    最近更新 更多