Using the replace function in firestore security rules

后端 未结 3 480
醉梦人生
醉梦人生 2021-01-16 10:49

I\'m struggling with Firestore security rules. I want to check on a value that needs the replace function, i.e. an e-mail address. I can find some documentation in the gener

相关标签:
3条回答
  • 2021-01-16 11:12

    A little late, but you can simulate the replace function on a string with this one :

    function replace(string, replace, by) {
      return string.split(replace).join(by);
    }
    

    So you need to define this function in your firestore.rules file and then you can call replace(request.auth.token.email, '.' , ',') to get the same result as request.auth.token.email.replace('.' , ',') in javascript.

    0 讨论(0)
  • 2021-01-16 11:14

    There are two reasons why you might have been having issues.

    1. The replace function was added to Security Rules after you asked your question.
    2. The replace function uses regular expressions for the first argument and so matching on '.' will match literally everything.

    Consider instead using: request.auth.token.email.replace('\\.' , ',')

    0 讨论(0)
  • 2021-01-16 11:26

    var emailSanitized = email.replace('.' , '.'); db.collection('someCollection').where('members.' + emailSanitized, '==', 'admin')

    0 讨论(0)
提交回复
热议问题