A few weeks ago I went with the whole Aurity team on the second HackerHouse, and this time was on Bali. To make this trip even more productive, we booked a modern coworking space where we held five workshops. Each workshop was prepared and presented by developers where we shared our knowledge and experience. We have recorded all workshops, and now we are sharing the first one with you.

In the following tutorial you’ll get an overview of how to create and deploy Firebase Cloud Functions and Firestore rules. By creating sample functions and rules in the video, we showed the development process step-by-step so you can follow along and expand your knowledge of these Firebase features.

Installing NodeJS and Firebase

To begin, we need to make sure we have Firebase tools installed. We use the Firebase CLI that provides a variety of tools for managing, viewing, and deploying to Firebase projects.

As you will see in instructions above, to install and configure the Firebase CLI, you will need to have Node.js on your machine. Once you have it, you can install the global Firebase CLI using npm by running the following command:

npm install — g firebase-tools

Choosing the Project

To login to your projects, run the command firebase login in your terminal using your Google account. Once your credentials are approved, type the command firebase list to get all of your projects displayed. To select the project you want, type the project ID after entering the command firebase use . If you don’t know the ID of the project, or the ID is too long, you can use the firebase use --add command to select the project from the clickable list and change its alias.

Creating Firebase Project Directories

If we want to create a Firebase project locally on our machine we can run a function firebase init in chosen directory and it will automatically create a file firebase.json with default configuration for the projects.

Deploying a Firebase Function

All functions will be deployed in the directory called “functions” where you have the index.js file. In this file, you will export all functions you want to add to the project. To deploy and overwrite added functions, go to your terminal and run the command firebase deploy . If you want to deploy the function without overwriting use

firebase deploy --only functions:name_of_the_function

In our tutorial we used a Firebase Cloud function for sending invitations. This function gives an option for a user to invite someone to a certain project with selected privilege by sending an email invitation. Once the function is triggered you will be able to see all logs in your Firebase dashboard. This way you will be sure if the invitation was sent correctly.

Calling the Firebase Function

There are two direct ways to call a function:

Call Functions from your App which includes an authentication token Call Functions via HTTP Requests by using functions.https

In the invitation function mentioned above we used Cloud Storage triggers. In the tutorial you will see that we used function.firestore along with a document called “invitation” which represents the name of the collection where the document is located. Whenever we create a new document inside the collection we extract the data that was saved in the database (eg. email, role, project name). We use Nodemailer to send email invitations to the new user. Be aware that you need to prepare and add the code of the email template in order to have styled email. Once the email is sent you will see a notification in the console.log that the invitation was sent.

For triggering a function using HTTP Request we created a function called “notify”. This function uses previously collected data (email, role, project name) to send a specific notification to the user informing him about changes in his project. Depending on the notification and response, we create an email that is sent to the user.

Using functions.https we created a function that handles HTTP events. The event handler for an HTTP function listens for the onRequest() event, which supports routers and apps managed by the Express web framework.

To see the url that triggers the function go to functions page in Firebase console.

Creating and Deploying Firestore Rules

To access the rules go to your Firebase Database and select rules tab. By default the access to the database is restricted so you can’t access it unless someone allows you with the rule. To write a Firestore rule properly you need to add match statements, which identify documents in your database, and allow expressions, which control access to those documents.

Besides the console, rules can be written in the Firebase project. The firebase.json file in the rules directory consists a path to the rules file and it is used for rules deployment. To deploy the rules use the command firebase deploy or firebase deploy --only firestore:rules .

Take a look at more examples of how rules can be used are presented in our tutorial video below.

Creating and Deploying Firebase Cloud Functions and Firestore Rules

After watching this tutorial, you should be able to understand how to create and deploy Firebase Cloud functions and how to secure your Firestore database. If you have any questions, please feel free to drop them in the comment section and we will answer as soon as possible.

Enjoy your code!