英文:
Firestore Security Rules Invalid variable name: request
问题
The issue in your code is that you're using HTML entity codes like > and & instead of their actual characters. Here's the corrected code with the proper translations:
rules_version = '2';
service cloud.firestore {
// Check if user is authenticated
function isAuthenticated() {
return request.auth != null;
}
match /databases/{database}/documents {
function isUserCompanyAdmin(companyID) {
return isAuthenticated() &&
request.auth.token.role == 'admin' && request.auth.token.companyID == companyID;
}
match /companies/{companyId} {
allow read, write, update, delete, create, get: if isUserCompanyAdmin(companyId);
}
}
}
The issue was mainly with > and & which have been replaced with > and &.
英文:
I'm having an error when writing a security rule that verifies if the requestor can make an action. By I'm getting:
> Invalid variable name: request.
Here is my code:
rules_version = '2';
service cloud.firestore {
// Check if user is authenticated
function isAuthenticated() {
return request.auth != null;
}
match /databases/{database}/documents {
function isUserCompanyAdmin(companyID) {
return isAuthenticated() &&
request.auth.token.role == 'admin' && request.auth.token.companyID == companyID;
}
match /companies/{companyId} {
allow read, write, update, delete, create, get: if isUserCompanyAdmin(companyId);
}
}
}
What's wrong?
Thanks
答案1
得分: 1
如果我记得正确,request 仅在 match 子句内可用。因此,您可以将 request 传递给该函数,或将其移到 match 子句内。
英文:
If I recall correctly, request is only available inside match clauses. So you can either pass request to the function - or move it into a match clause.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论