Define custom projectId in JS for emulator
See original GitHub issue[REQUIRED] Environment info
firebase-tools:
- firebase emulator
- firebase js sdk 7.8.0
- firebase cli 7.12.1
Platform:
- macOS
[REQUIRED] Test case
<script>
document.addEventListener("DOMContentLoaded", function () {
// this doesnt work
var firebaseConfig = {
authDomain: "localhost:5000",
projectId: "project-name-test"
};
// Initialize Firebase
const app = firebase.initializeApp(firebaseConfig);
// this works great
firebase.firestore().settings({
host: "localhost:5002",
ssl: false
})
</script>
[REQUIRED] Steps to reproduce
[REQUIRED] Expected behavior
It would be great to init the app with a custom projectId so that the projectId used in Tests against the emulator can be used in some test html / js interface stuff. I would like to keep all my tests and code using a different projectId just to prevent the issue of someone accidentally running them against the live system. Yes the firestore.rules should prevent any major issues but its still possible one of our developers could use their credentials and bearer token against a live url without realising.
[REQUIRED] Actual behavior
I get “Your API key is invalid, please check you have copied it correctly”
Maybe theres a way to do this already and I just don’t understand. The documentation for all this advanced usage of the local emulator is a little lacking. I spent all day trying to figure out why my GET requests to the emulator were not working:
Locally with emulator: GET http://localhost:8080/emulator/v1/projects/example-project/databases/(default)/documents/user/alice Doesnt work. Response:
Not Found
Only to realise its because the /emulator/ part is only required when you call:
DELETE http://localhost:8080/emulator/v1/projects/example-project/databases/(default)/documents
To clear all documents.
It would be awesome if you could include an example project with tests that also sets up a sane local test environment for everything from auth, cloud functions and firestore calls. 😃
Issue Analytics
- State:
- Created 4 years ago
- Reactions:4
- Comments:6 (4 by maintainers)
@madhavajay thanks for explaining all that.
So if you want to change the projectId with the automatic init scripts, just use the
--project
flag like this. For example using my projectfir-dumpster
:When I curl the init file I get something like this:
Now let’s say I do this with a totally made up projectID like
my-fake-project
:You can see the emulators start up just fine but they warn me that they couldn’t fetch a configuration for this project. That makes sense, it’s not real!
When I curl the init script now I can see it doesn’t do anything:
So that leaves me to run
initializeApp()
on my own when my page loads. I don’t need amessagingSenderId
or even astorageBucket
because none of those things are going to work on this fake project.I think a good improvement would be for
__init.js
to automatically point your services at the emulator when possible. I will work on that.Anyway I hope I have answered your questions! I am going to close this issue, but let me know if you have more and I am happy to discuss.
@samtstern thats really awesome. I knew there must be a way but was having trouble interrogating the dynamic config when using the --project param but didn’t think to just hit the .js end point. Very smart. I agree this would be a great addition to the system to automatically handle the correct connections. Also is there a reason that we need to manually override things like this:
Seems like it would make more sense to do this as part of the initializeApp config or as you mentioned automatically in __init.js.