Authentication token property "firebase" not defined on test app's instance
See original GitHub issue[REQUIRED] Environment info
firebase-tools: 8.4.1
Platform: Windows 10
[REQUIRED] Test case
Firestore rules file:
rules_version = '2';
service cloud.firestore {
match /databases/{database}/documents {
match /{prefix=**}/{document} {
allow read, write: if request.auth.token.admin == true;
// TODO Verify that this is not a bad practice
}
match /articles/{articleId} {
allow read: if request.auth.uid != null;
allow write: if "sign_in_provider" in request.auth.token.firebase
&& request.auth.token.firebase.sign_in_provider != "anonymous";
}
}
Test Suite:
import * as firebase from "@firebase/testing";
import * as fs from "fs";
const projectId = "firestore-emulator-example";
const data = {
"title": "hello world"
}
const rules = fs.readFileSync("../firestore.rules", "utf8");
before(async () => {
await firebase.loadFirestoreRules({projectId, rules});
});
beforeEach(async () => {
await firebase.clearFirestoreData({projectId});
});
after(async () => {
await Promise.all(firebase.apps().map(app => app.delete()));
});
describe('Articles collection', () => {
it('should allow authenticated users to create an article', async () => {
const db = firebase.initializeTestApp({"projectId": projectId, "auth": {
"uid": "user-id",
"email": "user@example.com",
"token": {
"sub": "user-id",
"aud": "test-project",
"firebase": {
"sign_in_provider": "password"
}
}
}}).firestore();
const articles = db.collection("articles");
await firebase.assertSucceeds(articles.doc("article-id").set(data));
});
});
[REQUIRED] Steps to reproduce
- Create a firebase project with firestore
- Use the firestore rules above
- Run firestore emulator
- Run test suite from above
[REQUIRED] Expected behavior
The tests runned locally fail with false negative and with the error “Property firebase is undefined on object.”. The same rules applied on the cloud rules succeed with the expected behavior.
[REQUIRED] Actual behavior
It is expected that the emulator’s instance is mirroring the correct behavior like in the actual production environment. The property firebase
should, since it was provided by the authentication object auth
, be defined and not null. The object structure is copied from the browser tests that can be run directly when modifying the rules.
Further Information
See https://groups.google.com/g/firebase-talk/c/g79uxsyOf3E/m/TvXnaqcjAwAJ
Issue Analytics
- State:
- Created 3 years ago
- Comments:10 (7 by maintainers)
Top Results From Across the Web
Create Custom Tokens | Firebase Authentication - Google
The Firebase Admin SDK uses the IAM API to sign tokens. This error indicates that the IAM API is not currently enabled for...
Read more >Uncaught ReferenceError: Firebase is not defined
As the firebase object is defined differently it does not find it. In my case, I need to update the library to be...
Read more >OAuth 2.0 for Client-side Web Applications - Google Developers
This document explains how to implement OAuth 2.0 authorization to access Google APIs from a JavaScript web application.
Read more >Using Firebase Authentication - FlutterFire
Before using Firebase Auth, you must first have ensured you have initialized FlutterFire. To create a new Firebase Auth instance, call the instance...
Read more >next-firebase-auth - npm
We treat the Firebase JS SDK as the source of truth for auth status. When the user signs in, we call an endpoint...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
@malliaridis (and anyone else following along here) this should be mostly addressed by this PR: https://github.com/firebase/firebase-js-sdk/pull/3876
In the next release of
@firebase/rules-unit-testing
theoptions.auth
object will have strong types that help you discover and use all the available fields. I hope that helps!Hello Scott, that solved my problem, thanks. 👍
The documentation varies a bit from the actual implementation at this point. Therefore the confusion with the auth object from https://firebase.google.com/docs/rules/rules-and-auth.
Maybe I could provide some suggestions to improve that part? Let me know. 😃