Search code examples
angularfirebasegoogle-cloud-firestorefirebase-tools

Firestore emulator authentication issue leading to no change in data


My Angular (TypeScript) app, which runs fine on cloud Firestore and appears to run well on the local emulator, does not write data with the Firestore emulator.

If I add data with my app, shut down my app (just the browser window, or if I kill the app (ng serve)) and restart, the data is there. If I look in my emulator Firestore console, the data is not there.

If I look at the requests, I see each request failing, and then a duplicate request succeeding. The reads seem to be fine with this (retry logic I presume), but the writes, even though the request shows successful create, it is not in the database.

The first create request (which fails) shows me the reason as failing in the security rules apparently with request.auth.uid = null (or the get to the user table is failing which is the second half of that rule).

Immediately after (the same second per the timestamp), it shows a duplicate create request (same docID) succeeding and shows the same line in the rules succeeding (specifically green arrow on that line).

In the end, the data does not show in the console, and if I export the data, shut down the emulator, and restart the emulator (importing the data), the data is not there. Maybe this is working as designed, and I'm fine if I have to use the console to add data that I want to come up when my emulator runs. It is just more convenient to add or modify data with the app, and I wanted to be sure I didn't just have a parameter somewhere that I needed to change.


Solution

  • Thanks to Chris and others, I stumbled upon it. Using the demo_xxx project in the environments file is great, but you must also use it in starting your emulators. If you do not, all seems to work, but the UI is out of the picture (i.e., what you add is there, but the UI can't see it). So since I had all other configuration correct; I just changed it, so that when starting the emulators, my command is now:

    firebase emulators:start --import=./globData --project=demo-clerk
    

    It seems to work even though firebase projects:list does not show it. So my problem was that the emulators must start with the project as specified in the configuration (environment in my case). This gives all the benefits of the emulators (including not accidentally getting real services during test) and it all works just like the real deal.