How to create a firebase project with same structure as an existing one? - firebase

I'm working on a product which uses Firebase as its backend. Since firebase exposes the API keys to the user so that could be a security issue. So, after doing some research I've set the database security rules along with API keys restrictions.
But, now I'm unable to use it in local development as well. I was thinking of creating another firebase project and use that as a testing environment and use the existing one as production.
Since the existing project has a lot of data and users. I want everything similar in the new firebase project as well. But I'm unable to find an efficient way to do so. Can anyone please suggest what would be the best option here? Should I create a new testing environment or is there a way to allow me to use the keys locally without it causing a security concern?
Any help would be great. Thank you for your time.

There is no specific command to replicate one project to another, but you can build the necessary functionality yourself with each product's APIs.
For porting users between projects you can use:
The Firebase CLI, which has auth:import and auth:export commands.
The Firebase Admin SDK, which has commands to list all users and import a list of users.
For transferring data between the projects, you can use the API of the relevant database to read/write the data.

Related

Create and configure Firebase project programmatically

I am building an automation process for my app, which includes creating several new Firebase projects. (After a lot of investigation, I am sure that several projects are needed, and that it cannot be done with just one project).
I am looking for a way to create a new Firebase project from scratch, and enable Phone Auth, Cloud Firestore, and Cloud Storage, along with security rules, programmatically.
I have taken a look at the Firebase Management REST API, which indeed shows a way to create a new Firebase project and link it to an existing GCP project, but couldn't find a way to configure the project itself through the API (Authentication, Firestore, Storage, etc.).
Is there any way to create and configure a Firebase project from scratch, using an API/SDK or CI/CD of some kind?
Thank you!
Have you looked into the firebase-admin SDK? It is a backend-only API because it needs your private key to authenticate against and can therefore not be used in the app directly (shipping your private key with the app would be pretty big security issue!), but as I understand it, creating new firebase-projects is something of a backend activity for you anyway?!
Look at https://firebase.google.com/docs/auth/admin/ for the API's documentation and what you can do with it.
So after a long research, it seems that we can combine the Firebase CLI and the Firebase Admin SDK in order to achieve it.
We can create a new project using the command
firebase projects:create NEW_PROJECT_ID
And then to configure all of the necessary configurations through the admin SDK

How to set up a seperate development environment in firebase?

I published my first app using firebase and now I want to continue developing on this app. For that I dont want to test changes in the production firebase project so I thought about creating a new Firebase project for development. Is this a good practise?
If so, what do I need to change in my code from the published app? Only the google-services.json file from firebase?
And can this project use the same package name?
You may occasionally need to use the same APIs to access numerous projects, such as multiple database instances. In most circumstances, a central Firebase application object maintains all of the Firebase APIs' setup.
As part of your standard setup, this object is created. When you need to access many projects from a single application, you'll need to create a separate Firebase application object for each one. It's up to you to get these additional instances started.
As a recommendation from the last comment, here is the link on how to configure Multiple Projects using Firebase, furthermore here is a link of how to Understand Firebase Projects, that could also help with your main goal.
I found tutorials using Firebase and Flutter that could also guide you:
Multiple Firebase Projects
Multiple Build Environment

How to administrate this estructure on Firebase?

There are multiple companies using one application. So I want to have, or know if this is the best way, to create a project for each company, all this with the Firebase API, to create everything programmatically. But I think this is too difficult to do and get that structure. The truth is that the documentation about the Rest API to create projects is not very clear and I am getting lost. One of the things that I want to structure projects like this is to know how much use is made of each project, calculate requests and use of storage. But I don't know if there are other ways to do this.
It's not possible to create a new project for Firebase using the Firebase API, what is possible is to list projects, and create/list apps within an existing project using the Firebase Management API.
For monitoring usage, you can use Cloud Monitoring to assess the usage of each individual app, per service.

How to run different cloud functions for seperate apps in the same Firebase project?

For example, if I wanted to use the functions.auth.user().onUserCreate() trigger, is there any way to make it trigger different functions for different apps that are all in the same Firebase project? Is the only way to fix this to create separate Firebase projects for each app? The issue with that is that I need them to access the same database.
It's not possible to know in a Cloud Functions trigger which app a user used when they created their account. Firebase Auth accounts don't have a sense of "ownership" with respect to multiple apps per project. All apps in a project share the same users with the same permissions.
What you can do instead is have each app write something unique in your database after the account was created, then use that to determine what that user should do later on. Either that, or use different projects if your apps don't actually need to share the same set of users.
The only possibility that I think that might help you is organizing your Cloud Functions in different files, which will be then loaded per project. So, for example, in your Project Alpha, it will run the Cloud Functions from the foo.js file, while in the Project Beta, it will run the functions from the bar.js and the same logic for other projects and Cloud Functions.
This way, even though all users are sharing the same database, it will trigger the function based in the file that you requiring in your application. You can check for tips in organizing your Cloud Functions in multiple files and calling them individually, in this official documentation here:
Organize Functions
I believe this way might work for you, so, I would recommend you to check the documentation and give it a try splitting the functions in multiple files and calling each specific files, on their specific applications only.
Let me know if the information helped you!

Firebase tools use cases

could somebody explain use cases for using firebase tools. I understand that we can use them to connect to Firebase Realtime database and for authentication. But what I cannot grasp is: do we use it on our local machine and then deploy to google or is it also useful when hosting on other servers? I would love to hear more use cases for using the tools.
Thank you.
The Firebase tools/CLI provide commands to interact with your Firebase projects from a shell/terminal.
You'd typically run it on your local machine to make changes to the database, update files for hosting, update your security rules, import/export users, etc.
For a full list of its commands, see the reference documentation for the Firebase CLI.

Resources