匿名使用Firebase进行身份验证

时间:2020-06-26 20:30:37

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

我目前正在尝试让用户通过匿名身份验证实现对Firestore数据库的读取访问。

我要这样做的原因是因为我不断收到电子邮件,说“您的Cloud Firestore数据库具有不安全的规则”,而且我不想让用户登录。

为解决此问题,我在打开应用程序时使每个人都成为匿名用户,但我在编写批准对匿名用户的读取访问权限的规则时遇到了麻烦。

下面的功能正在工作:

func signInUser(){
    let auth = Auth.auth()

    auth.signInAnonymously{(result, err) in
        if let err = err{
            print(err.localizedDescription)
            return
        }
        print("User Logged in anonymously")
    }
}

如何在Firebase控制台上编写规则,以仅允许对Firestore集合进行读取访问。

这也是个坏主意吗?

当前规则:

service cloud.firestore {
  match /databases/{database}/documents {

    match /{document=**} {
  
      allow read: if true
    }
  }
}

1 个答案:

答案 0 :(得分:1)

我建议阅读documentation on security rules,以了解用户身份验证如何与安全规则一起工作。

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

这是一个好主意完全取决于您。您必须决定是否要让所有用户都能够读取所有数据。