无需身份验证即可访问Firestore数据库

时间:2020-10-24 10:20:09

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

在我的应用中,我想检查用户登录之前是否存在带有相同电子邮件的文档(其中'email'是我的'user_data'集合中的文档字段)...

我已经完成了执行该操作的代码,但是即使用户尚未登录(并且在我的Firestore规则中我说allow read, write: if request.auth != null;),它也需要访问我的Firestore数据库

那么,如何在不允许所有人使用read的情况下更改Firestore规则?

2 个答案:

答案 0 :(得分:1)

更改

allow read, write: if request.auth != null;

allow read, write: if true;

答案 1 :(得分:1)

我不建议将您的Firebase数据库安全性规则公开。

但是我能理解你的情况。我可以给你个主意。

这就是我在不更改安全规则的情况下实现它的方式。

要在单击按钮后进行检查时:

//SIGN UP

try {
  UserCredential userCredential = await FirebaseAuth.instance.createUserWithEmailAndPassword(
    email: "USER ENTERED PASSWORD",
    password: "SuperSecretPassword!"
  );
} on FirebaseAuthException catch (e) {
  if (e.code == 'email-already-in-use') {
    print('The account already exists for that email.');
    // Tell User That It already exists!
  }
} catch (e) {
  print(e);
}

// Or SIGN IN

try {
 UserCredential userCredential = await FirebaseAuth.instance.signInWithEmailAndPassword(
    email: "barry.allen@example.com",
    password: "SuperSecretPassword!"
  );
} on FirebaseAuthException catch (e) {
  if (e.code == 'user-not-found') {
    print('No user found for that email.');
  } else if (e.code == 'wrong-password') {
    print('Wrong password provided for that user.');
  }
}

何时要检查电子邮件文本字段中的键入内容:

UserCredential userCredential = await FirebaseAuth.instance.signInAnonymously();

您可以匿名登录并阅读数据库!阅读并过滤掉后,注销用户。

或者,如果您还没有说服的话,那就走了,

引用:https://firebase.google.com/docs/rules/basics#content-owner_only_access

read设为公开,将write设为授权用户。

希望能满足您的情况!