Using the replace function in firestore security rules

社会主义新天地 提交于 2019-11-27 19:37:52

问题


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 general security docs, but that does not seem to work with Firestore.

For example this works:

allow write: if resource.data.members.data[(request.auth.token.email)] in ["admin"];

but this doesn't (and I changed the key in the members object accordingly):

allow write: if resource.data.members.data[(request.auth.token.email.replace('.' , ',')] in ["admin"];

Another option would be to have a way to use dots in the address of a query, so they don't have to be replaced like this:

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

Any ideas here?


回答1:


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.




回答2:


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



来源:https://stackoverflow.com/questions/47070823/using-the-replace-function-in-firestore-security-rules

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!