Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Compilation errors in firestore.rules: Unexpected 'if'

I have this compilation error in my firestore.rules:

Compilation errors in firestore.rules:
[E] 35:7 - Unexpected 'if'.
[E] 38:7 - Unexpected 'else'.
[E] 42:3 - Unexpected '}'.

The excerpt from the firestore.rules is as follows:

service cloud.firestore {
  match /databases/{database}/documents {

    match /requests/{id} {
     allow read: if isSignedIn() && ((getUserKennung() in resource.data.users_read) || userIsXY());
     allow write: if isSignedIn() && ((getUserKennung() in resource.data.users_write) || userIsXY());
    }

    function getUserKennung(){
     let email = request.auth.token.email;
     if(email.lower().matches('.*@provider[.]org')){
       return email[0:7].upper();
     }
     else {
       return 'ERR: Invalid usermail';
     }
    }

    function userIsXY(){
      return request.auth.token.email == 'XY@provider.org';
    }

    function isSignedIn(){ 
      return request.auth != null;
    }
  }
}

I have no clue why the error occurs.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

I don’t think if...then...else is allowed in custom functions in security rules, so you might want to try a ternary expression like this:

function getUserKennung(){
 let email = request.auth.token.email;
 return email.lower().matches('.*@provider[.]org')
   ? email[0:7].upper()
   : 'ERR: Invalid usermail';
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading