1

I'm trying to create a rule in Firestore that restricts usage to all CRUD operations for users that belongs to a specific domain, My issue is that seems like contains clause doesn't exist in the rule context.

this is my rule:

service cloud.firestore {
  match /databases/{database}/documents {
    match /{document=**} {
      allow read, write: if isUserAndSignedIn();
    }
  }

  function isUserAndSignedIn(){
    return request.auth != null && request.auth.token.email.contains('domain.com')
  }
}

the error I'm getting is: Error: simulator.rules line [9], column [36]. Function not found error: Name: [contains].

I also tried with indexOf like

request.auth.token.email.indexOf('domain.com')!=-1 request.auth.token.email.endsWith('domain.com')

Rim
  • 1,735
  • 2
  • 18
  • 29
pedrommuller
  • 15,741
  • 10
  • 76
  • 126

1 Answers1

4

request.auth.token.email is going to be a String, and according to the reference docs, String doesn't have a method called "contains". It does have a method called matches for using regular expressions, and you can see the documentation has a handy bit of sample code:

'user@domain.com'.matches('.*@domain[.]com') == true

Which you should be able to adapt to your case:

return request.auth != null && request.auth.token.email.matches('.*@domain[.]com')
Doug Stevenson
  • 297,357
  • 32
  • 422
  • 441