You Might Not Need a Backend

Surya Kant Bansal
Jan 3 · 9 min read

In any graphical application, there are two major components, Frontend & Backend. If you are building a new application, you would probably use a framework or library like Angular or React to write a powerful Frontend application, you would use another framework like Express.js, Django or Spring to write a powerful Backend application and along with that, you would use a database service like MongoDB, PostgreSQL or MySQL. Now if you think about it, these are 3 separate applications that you need to create for your project and you would also need to deploy these 3 applications on multiple cloud resources.

If you are building a small project which does not really require high computation power, the development and deployment of this architecture might cost you a lot more time and funds than you expect. There is an alternative way to build such a project which is simpler, easier and possibly cheaper. You can use Firebase with a React Frontend. Firebase provides services NoSQL database, Cloud Functions, Hosting, Analytics and much more and it also provides a JavaScript SDK that can be used directly inside the React application to communicate with the Firebase services easily.

In this blog, we will create a simple “Inventory Application” in React using Firebase services for database, authentication, authorisation and hosting. I’ll start by creating a base project from my react-boilerplate template repository. This boilerplate has a minimal React application setup with all the important development features like TypeScript, ESLint, Prettier, Jest and other project configurations which makes your development experience seamless.

Development

In this section, we’ll go over creating the application user interface, linking the application to Firebase and rendering data on the user interface that is received from Firebase.

Once we have the React application set up, before jumping into Firebase, I’ll create the user interface for the application using some mock data. I want the inventory application to be configurable, so, I also create a configuration object for the application that we can later put in Firebase. The configuration object will make it easier to make some simple changes in the application without redeploying it. In my case, I am creating a headers object that is used to render the inventory table.

Using this configuration, I render a table that can be customised by just changing a few configurations instead of rewriting the code. Note that the keys inside an item object correspond to the keys of the headers, this is how the data is being populated in the table. This is how the UI looks now.

I also added a “SIGN IN” placeholder button and a filter that refines the items in the table.

We begin with creating a project on the Firebase console. You can follow the simple on-screen steps to create the project. Once the project is created, the next step would be to create an app inside the project. To do this, you need to go the project settings and at the bottom, you’ll find an option to create a new app where you can select the option for a web app. Then you just follow the on-screen instructions to create the app. After the app has been created, you need to install the firebase-sdk to the React application and also install the firebase-tools CLI.

You would also find a configuration object in the Firebase application settings that looks like this:

Next, we will use the Firebase CLI tools to log in to the firebase account and link our React project to the Firebase project.

This command will use your browser log into your Firebase account.

This command is to be run inside the project directory. This command will ask about the Firebase project that is to be linked with the React project. It will also ask which services this project will be using. We will use Cloud Firestore and Hosting services. Cloud Firestore is the NoSQL database service and Hosting service can be used to deploy our application from Firebase itself. You can find the required answers to the project setup questions below.

Now, all we need is to create a local firebase module with all the required settings that can be used across the React application.

Note that I have used dotenv-webpack to pick the Firebase configurations from an environment file.

The connect and disconnect functions can be used to create and terminate the connection with firebase. Now, we can import db from this module to communicate with Cloud Firestore database. To understand more about Cloud Firestore and how to use it from the Firebase SDK, you can visit the official documentation. The SDK provides very easy ways to perform CRUD operations as per the developer's requirements.

Once the setup is done and we have the db instance to connect to Cloud Firestore, I created a React context that will provide functions throughout the application to perform CRUD operations on Cloud Firestore. I created 2 collections in Cloud Firestore, one for the headers configuration and another for the items to display. As you may have noticed earlier, I am storing the items as a map between its ID and its values. Now, to create new items, instead of keeping track of numbered IDs, we can use the uuid package to generate universal IDs. These IDs will be used as document IDs in the Cloud Firestore. Cloud Firestore will also require you to write some access rules, you can just set them open to all for now.

Now, with the React context in place, I created a modal form to add more items in the table and I also added edit and delete action buttons in the table itself.

Added action buttons and a modal form

Authentication and Authorisation

The next thing this inventory dashboard requires is proper authentication and authorisation. Firebase Authentication provides its own authentication modules inside the firebase-sdk that are really easy to use. Firebase also provides some UI libraries that includes pre-built authentication interfaces but we won’t be using that for this project. We will use a simple Google OAuth2 authentication process for our application and we will use Cloud Firestore to handle authorisation.

The first thing we need to do is to enable Google sign-in from the Firebase console under “Authentication” > “Sign-in method”. Then, we can create a GoogleAuthProvider instance using the firebase-sdk. I did this in the same local firebase module that I created earlier.

Next, I created another context called AuthContext.tsx to handle authentication actions and store user information. In this context, I have two functions that handle sign-in and sign-out. The user information returned by the OAuth API after sign-in is stored inside a state variable.

These functions can now be linked to a sign-in/sign-out button click events. In addition to this, we also need to handle cases in which the page reloads. When the page reloads, we need to check if the user was already signed in or not and set the state accordingly. We can do this using a useEffect hook.

This sets up the basic authentication where any user can sign-in to the application using his/her Google account.

With the authentication in place, now we want some of our users to have administrator access so that they can alter the inventory. The first step to do this is to create another collection in Cloud Firestore called users. This collection will store some information regarding the application users mapped using their UID. UID is generated by Firebase and it is unique to each user and Firebase project.

Once a user signs into the application, you can see his/her details (including UID) in the “Authentication” tab in the Firebase console. Now, you can copy this UID, make a document, inside the users collection, with ID as the UID and inside that document, create a boolean field called admin.

In the application AuthContext.tsx , we can now fetch the data from the users collection and check if the current user UID has admin access or not and the application UI can be manipulated based on that information.

We also need to modify the Cloud Firestore rules so that only administrators have the write access. To do this, you can modify the firestore.rules file with the following code which restricts write access to all collections except items for which, write access is only provided to administrators.

You can deploy these rules using the following command:

Hosting

Firebase also provides hosting service which can be used very easily. First, check that the public directory is set to build in the firebase.json file. Next, you need to compile the React application by running yarn build. Now you can run the following command to deploy your application.

Conclusion

https://github.com/skb1129/inventory-app

You can find the code for this project on the repository mentioned above. I hope this project helps us get a headstart for our future projects based on Firebase and React. I encourage you to star, fork and contribute to this template repository to make it even better.

“Intelligence is the ability to avoid doing work, yet getting the work done.” — Linus Torvalds

Thank you for reading this!

The Startup

Get smarter at building your thing. Join The Startup’s +793K followers.

Sign up for Top 10 Stories

By The Startup

Get smarter at building your thing. Subscribe to receive The Startup's top 10 most read stories — delivered straight into your inbox, once a week. Take a look.

By signing up, you will create a Medium account if you don’t already have one. Review our Privacy Policy for more information about our privacy practices.

Check your inbox
Medium sent you an email at to complete your subscription.

Surya Kant Bansal

Written by

Software Developer

The Startup

Get smarter at building your thing. Follow to join The Startup’s +8 million monthly readers & +793K followers.

Surya Kant Bansal

Written by

Software Developer

The Startup

Get smarter at building your thing. Follow to join The Startup’s +8 million monthly readers & +793K followers.

Medium is an open platform where 170 million readers come to find insightful and dynamic thinking. Here, expert and undiscovered voices alike dive into the heart of any topic and bring new ideas to the surface. Learn more

Follow the writers, publications, and topics that matter to you, and you’ll see them on your homepage and in your inbox. Explore

If you have a story to tell, knowledge to share, or a perspective to offer — welcome home. It’s easy and free to post your thinking on any topic. Write on Medium

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store