首页 > 解决方案 > Firebase Google 以身份验证中的用户身份登录

问题描述

我正在开发一个 Xamarin.Forms 应用程序。

多亏了 NuGet Plugin.GoogleClient ,我已经成功实现了 Google 登录。

问题是当我想修改 Firebase 项目规则时,只有经过身份验证的用户才能访问 Cloud Firestore 数据库。在我的 Firebase 控制台中,该项目似乎没有任何用户:

控制台1

但在应用程序中,我使用我的 Google 帐户没有问题:

在此处输入图像描述

如何验证 Google 登录帐户,以便它们可以出现在用户中,以便我可以修改 Cloud Firestore 的规则?

编辑:这些是我的 Firebase 规则:

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

在我使用 Google 登录后,我会立即获得Plugin.CloudFirestore.CloudFirestoreException: 'PERMISSION_DENIED: Missing or insufficient permissions.'.

但是,如果我将规则设置为 just true,我可以毫无问题地使用我的 Google 帐户,但对于异常状态,它没有经过身份验证 (?)。

标签: firebasexamarinfirebase-authenticationgoogle-signin

解决方案


要仅允许经过身份验证的用户访问 Cloud Firestore 中的数据,请查看有关所有经过身份验证用户的安全规则的文档。从那里:

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

即使您在 Firebase 控制台中看不到用户,这仍然有效。只要他们有 Firebase ID 令牌,就会在这些安全规则中检查该令牌。


推荐阅读