javascript - 如何从 Angular 或任何其他 Web 平台获取 Firebase 安全数据

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

这是我在 firebase 中的安全规则和条件

rules_version = '2';
service cloud.firestore {
  match /databases/{database}/documents {
    match /Product/{document=**} {
      allow read: if true; 
      allow write: if request.auth.uid != null;
    }
  }
}

根据 firestore 文档,我们可以在 once by this method 检索数据。

db.collection("Product").get().then(function(querySnapshot) {
    querySnapshot.forEach(function(doc) {
        console.log(doc.id, " => ", doc.data());
    });
});

如何从 Firebase 检索 protected 数据。如果它具有有效的 uid,它将返回给您数据。在每个请求上发送我的用户状态或 uid 的正确方法是什么

最佳答案

What is the proper method to send my user status or uid on every request.

您必须组合 Security RulesFirebase Authentication .

因此,通过 Firebase 身份验证,您可以覆盖系统安全的身份验证部分(即确认用户身份),而通过安全规则,您可以覆盖授权部分(即授予对系统的访问权限)。

来自documentation :

To sign a user into your app, you first get authentication credentials from the user. These credentials can be the user's email address and password, or an OAuth token from a federated identity provider. Then, you pass these credentials to the Firebase Authentication SDK. Our backend services will then verify those credentials and return a response to the client.

After a successful sign in, you can access the user's basic profile information, and you can control the user's access to data stored in other Firebase products. You can also use the provided authentication token to verify the identity of users in your own backend services.

因此,具体来说,您需要选择所需的登录方法(即密码、电话号码、联合身份提供商,如 Google、Facebook 和 Twitter 等),然后实现一个(或多个)相应的登录您的应用程序中的函数。请注意,您还可以使用自己的身份验证机制,因为 JSON Web token (JWT) 可以在您自己的服务器上生成,并用于通过 signInWithCustomToken() 方法对用户进行身份验证。

登录后,身份验证 token 将自动包含在对 Firebase 后端服务(Firestore、Cloud Storage 等)的请求中。

“Firebase 身份验证入门”中的更多详细信息 page .

<小时/>

您还可以在网上找到几个教程:https://www.google.com/search?q=angular+firebase+authentication+example

关于javascript - 如何从 Angular 或任何其他 Web 平台获取 Firebase 安全数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61558284/

相关文章:

javascript - 为什么 Intl 和 Reflect 的默认字符串表示为 `[object Object]` ?

ios - 用户更改密码时 Firebase 注销所有帐户

angular - 在控制台中获取 'The timestampsInSnapshots setting now defaults to true' firestore 错误

angular - 我收到错误 "Spread types may only be created from object types.ts(2698)"

android - 如何在android中的firebase中创建唯一的id

google-cloud-platform - Google Cloud Firestore 与 Google Cloud Spanner 之间的区别?

javascript - 如何在 Opera 中设置样式显示?

javascript - 使用 AXIOS 在 JS 中未定义异步函数的结果

javascript - 如何在 underscore.js 中返回部分匹配的数组

javascript - 如何实时查询firestore?