0

I am trying to check if an User is present in my database by email, are there any firebase rules to do that? My User:

public class User {
    private String email;
    private String password;
    private boolean hasRightToDrive;
}

With this firebase rules I am not able to add users:

{
  "rules": {
    ".read": "now < 1640815200000",  // 2021-12-30
    ".write": "!data.exists()",  // 2021-12-30
  }
}

with this I am still able to add multiple users with same mail

{
  "rules": {
    ".read": "now < 1640815200000",  // 2021-12-30
    ".write": "data.exists()",  // 2021-12-30
  }
}
Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
  • Login process already have that it will throw an error that email already exist. – Mises Dec 04 '21 at 14:54
  • `Firebase: Error (auth/email-already-in-use).` This error you get if you catch it if not it will throw a console log. – Mises Dec 04 '21 at 15:03
  • If you not meant users by people who are logged in your application but persons who sends you their emails. Best way is to make firebase function where you take an email check are it allredy exist in database and throw an error if yeas. – Mises Dec 04 '21 at 15:15
  • Firebase security rules cannot check for the existence of a specific value under a parent node, as that'd require them to query all those nodes and that won't scale. The solution is to add an additional path where you use the (encoded) email address as the **key** of your child node. Since keys are by definition unique in their parent, there'll be no way for you to insert a duplicate in any way. For more on this, see some of the previous question on the topic that I linked. – Frank van Puffelen Dec 04 '21 at 16:02

1 Answers1

0

Login process using firebase will throw an error if user with email allredy exist: Firebase: Error (auth/email-already-in-use).

If you not meant users by people who are not logged in your application but persons who just sends you their emails. Best way is to make firebase function where you take an email check are it allredy exist in database and throw an error if yeas.

Using rules you could set an id equal to user email and use in rules function exists() but don't think you can set an doc ID witch '@' and '.' chars. So you will need to make an hash using user email address and set hash as id. Emails are allways in lowercase so you can set id like: example@gmail.com as exampleAgmailOcom where A = @ and O = .

Mises
  • 4,251
  • 2
  • 19
  • 32