The world of serverless is growing and when talking about the term serverless, Firebase is a name that comes to the mind of many developers and enterprises. But what is Firebase? We'll discuss what Firebase is, how to authenticate with it, and how to use it in your next React project. Excited? Let’s begin.

Firebase is a Google cloud service that provides backend services for mobile and web applications. It is a widely used option for developers for backend-as-a-service. It has a real-time database service so all the records are instantly updated whenever there are changes in the database.

Since React.js is one of the fastest growing front-end JavaScript technologies supported by a large community, React developers need fast and reliable backend services to make the most out of it. Thus, Firebase is a good choice as it comes with the following advantages:

Multiple ways of authenticating with your application.

It provides the Login Persist where you’ll stay logged in even after refreshing or closing the browser window.

Third-party provider support e.g. Google, GitHub, Twitter.

It provides secure ways to authenticate.

Manages user session (users will remain logged in even when the browser or application restarts).

All of this allows us to focus on the user and their data rather than the backend code to maintain the sign in protocols. However, Firebase has a limitation that it is mostly used for small scale applications.

Why is it trending?

In the era of startups, one needs to be fast to. This technology supports the creation of a Minimum Viable Product (MVP) which is a great way to attract funds for your startup. Here, Firebase provides the fast backend environment to build your app.

Is it really needed?

Firebase provides the complete backend environment without writing a single line of server side code. It is easy to setup and run with a wide range of third-party auth providers.

By now you must have understood the basics of what Firebase is, it’s advantages/disadvantages, etc. but how can we implement it? Let’s dig deeper into actually working with Firebase and creating our first Firebase app.

So let us start by first creating an account on the Firebase website. You will see something like this:

a) Click on "Go To Console"

b) Now click on 'Add Project.' After clicking, this will take you to the below page.

c) Enter the Project name and accept the conditions.

d) On the left, you’ll be able to see options like Authentication, Database, Storage, Hosting, Functions, and ML Kit under the “Develop” category. However, we’ll be implementing only “Authentication” in this article. Click on Authentication and you will see something like this.

e) Click on the sign-in method you want to select as your sign-in provider. This is where you’ll be able to find different third-party providers with which to authenticate. Enable Email/Password.

f) Now go back to “users,” click on the 'Add User Button,' and create a user manually. Here, I am using the email with a password combination (uou may use any ID with a password).

g) Now the last thing to do on the firebase side is to Click on Web Setup and copy the configuration to use it on front end for your React web application which will have the information like apiKey, authDomain projectId etc.

Integrating Firebase Into a React Application

Congratulations! You are done with creating your first app on Firebase. It is time to integrate it with React.js.

Now, open your terminal and type the following command:

After hitting npm start, your React application will be up and running on port 3000.

First thing we are going to do is to install Firebase, and we have a Node npm module to do that.

Open your terminal again and type in:

Now we’ll create a folder inside src (source folder) with a name config and, within that project, we’ll create a new file with the name firebase.js. Now paste the configuration which we copied from our Web Setup inthe Firebase Authentication section into the Firebase file.

We will import the Firebase module on the installed Firebase file.

Code in the firebase.js file:

After creating the configuration to connect our React project with Firebase, we will create two pages: one is the Auth Page/Home Page (the page which will appear when the user gets authenticated) and the Unauth Page/Login Page (the page which will appear when the user is not authenticated).

I have used Bootstrap for this project. We won’t be discussing this here as it is out of the scope of this article. However, you can either leave the Bootstrap classes or can use normal CSS for your design purposes.

On the login page, we will have a form which will accept email id and a password and that has a Submit button.

This is also the unauth page so when we login we should be on the Home Page and when we logout we should be on the Login page again.

We’ll now create an authListenerfunction which is always called when the authentication state changes. So when a user logs in, this function is called: authListener.

We are calling this within the componentDidMountlifecycle:

On the home page, we will have a button that will allow users to logout. For logout, Firebase provides a function, firebase.auth().signOut(). We will be using it as simple function which gets triggered on clicking the logout button:

Here's the home.js file:

With all these things in place, you will be able to create a Authenticated Login/Logout Page with Firebase for your React project.