【问题标题】:Angular app not able to access Firestore database?Angular 应用程序无法访问 Firestore 数据库?
【发布时间】:2020-03-03 17:00:59
【问题描述】:

我有一个带有适当 firebaseConfig 的 Angular 应用程序,并且正确配置了 apiKey。当应用程序初始化时,它应该去相应的 Firestore 并在那里收集电子邮件地址并将它们存储起来以供以后的用户身份验证。问题是应用程序无法执行此操作,除非有用户登录。这令人困惑,因为我认为应用程序可以访问 Firestore 数据库 没有某人已经过身份验证。我能想出的唯一解决方法是设置 Firestore 规则以允许全局读取访问。这样,当应用启动时,就可以保证可以访问其中的数据库和电子邮件。

我在这里错过了什么?

【问题讨论】:

  • 所以您打算存储所有电子邮件客户端以进行身份​​验证?这听起来很不安全,您会将人们的电子邮件暴露给访问您网站的任何人..
  • 我是新手,完全愿意接受建议!

标签: firebase google-cloud-firestore firebase-security


【解决方案1】:

如果你的安全规则是这样的:

// Allow read/write access on all documents to any user signed in to the application
service cloud.firestore {
  match /databases/{database}/documents {
    match /{document=**} {
      allow read, write: if request.auth.uid != null;
    }
  }
}

只有在用户通过身份验证后,他们才能访问数据。您可以将规则更改为以下内容:

// Allow read/write access on all documents to any user signed in to the application
service cloud.firestore {
  match /databases/{database}/documents {
    match /{document=**} {
      allow read, write: if request.auth.uid != null;
    }

        match /{collectionName}/{docId} {
      allow read: if collectionName == 'emailCollection';
    }
  }
}

这样,如果用户通过身份验证,则他们可以访问所有文档,如果集合名称等于emailCollection,则未经身份验证的用户可以访问它。

https://firebase.google.com/docs/firestore/security/rules-structure#overlapping_match_statements

【讨论】:

    【解决方案2】:

    你没有错过任何东西。如果您希望用户能够在不提前进行身份验证的情况下查询 Firestore 集合,则该集合将需要完全的公共读取访问权限。不可能编写安全规则来限制对特定应用程序或网站的数据库访问。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-11-06
      • 2019-09-26
      • 1970-01-01
      • 1970-01-01
      • 2019-12-25
      • 2018-04-27
      • 2015-11-21
      • 1970-01-01
      相关资源
      最近更新 更多