Restrict Firebase authentication to a certain domain

Viewed 3971

I want to restrict the Firebase Authentication only by ne mail domain name, to make this mail access to my application :

exemple@exemple.fr

I did this in the database side of firebase

{
  "rules": {
    ".read": "auth.token.email.endsWith('@exemple.fr')",
    ".write": "auth.token.email.endsWith('@exemple.fr')"
  }
}

I think i'm doing something wrong, thank you for your help.

2 Answers

Try this

{
  "rules": {
    ".read": "auth.token.email.matches(/.*@exemple.fr$/)",
    ".write": "auth.token.email.matches(/.*@exemple.fr$/)"
   }
}

this works for me and I am able to restrict logged in Google user to my org domain

rules_version = '2';
service cloud.firestore {
  match /databases/{database}/documents {
    match /{document=**} {
      allow read, write: if request.auth.token.email.matches('.*@domain[.]com');
    }
  }
}
Related