Restric firesotre CRUD to users within a specific

2020-03-30 06:38发布

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')

1条回答
我欲成王,谁敢阻挡
2楼-- · 2020-03-30 07:31

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')
查看更多
登录 后发表回答