@firebase/testing - How to use auth custom claims in firestore rules tests?
I think the answer above could be a little more specific.
The custom claim needs to be on the auth object when initializing the app. But when checking the custom claims in the security rules the custom claims are in the 'token' property (request.auth.token.admin
).
Initializing the app:
firebase.initializeTestApp({
projectId: "my-test-project",
auth: { uid: "alice", my_custom_claim: "foo", admin: true }
});
firebase.rules
...
match /posts/{postId} {
allow update: if request.auth.token.admin
}
...
Custom claims can be passed as top-level fields within the auth
object:
firebase.initializeTestApp({
projectId: "my-test-project",
auth: { uid: "alice", my_custom_claim: "foo" }
});
In the example from the question, this line:
const defaultAuthUser = {uid: "alice", email: "[email protected]", "token": {"admin": true};
Should be changed to:
const defaultAuthUser = {uid: "alice", email: "[email protected]", admin: true};