How to Setup Firebase Authentication in React

How to Setup Firebase Authentication in React

When you build an application, we want to provide a user personalized experience. This is made possible using user authentication, where user login into the app and access the personal information from an account or social feed.

In this article, we will get to know how to react to app authentication with a firebase. Before starting the authentication process let’s get a brief introduction of Firebase and react.

What is Firebase?

Firebase is a backend-as-service. It provides a developer variety of tools and services to help them develop a quality app and user base. It’s built using Google development infrastructure. Firebase categorized as a NoSQL database program, which stores the data in JSON like a document.

What is a React?

React is a front-end development tool that is open source, a JavaScript library for building user interfaces or UI components.

Basic system requirements for authentication

  • A Firebase account

  • NodeJS vs .x .x or higher installed with npm/yarn

  • Create-react-app global module to scaffold a react project

Create a React App

To create a new react app, first, we need to install a create-react-app by run the below command in your terminal window.

05b94ecc-e515-4f67-a1e1-26c7a05fa977.png

Start a new project using firebase

To get started you need a Firebase account. To sign up and log-in visit: console.Firebase.com. Once you logged in you will see the below screen.

1.png

Fill in the detail and click on create a project. Once the project created, you will see the below screen.

2.png

Click on the Add Project button. This leads to another screen which contains a form to fulfill to create a new project.

3.png

Go to the Authentication page.

Click on the “Sign-in method” tab and make sure you have Google-enabled as a sign-in provider.

1_4KLlgj8Woa8ctyqS2J9tUA.png

Scroll down a bit and also make sure you have localhost as an Authorized Domain:

1_PsA8HJ2xxleQ66KirLodQQ.png

On the top of the Authentication page, click on the “Web Setup” button.

Copy the config variable and paste it to a file called firebaseConfig.js. Export it afterward. It should look something like this:

c0c2f93c-9731-4b0a-b09b-8b943a5285e1.png

Keep this file stored in src/FirebaseConfig.js .

Now we’re going to install Firebase libraries and a Higher Order Component through NPM. Run the following command:

b30932eb-1c9c-486b-adfc-62bf8c6dbcfc.png

Currently, your application is a CRA default application We’ll modify the src/App.js component. Insert these four imports to the top of the file:

238fb811-eb0a-4f03-b8db-ef055b36dfd2.png

Initialize the Firebase app using the configuration:

2a313fca-76d8-4365-b02c-773e22e140a0.png

Note: To integrate Firebase with our React app, we only need the initializeApp() method to pass all the keys required to configure from the Firebase.

Setup the providers we want to support and access the auth library:

c5f9db6f-a7bb-4335-9d56-58e475e75935.png

Wrap the export of the App component using the withFirebaseAuth HOC:

5620f61e-4cce-4097-a5f1-7e113d385266.png

Now we can access the properties provided by the withFirebaseAuth HOC in the App component.

It provides us with the user, an error, and some signIn and signOut methods as properties.

So we can add this to our render method:

4d98a211-2cbc-4481-ab07-d6a539049bbd.png

And change the markup to ask the user to sign in or sign out:

19ecfd43-76d7-4b93-b7b5-41f50bb95cda.png

If you check on localhost:3000, you can see:

9cf4fc0b-e9c0-4cf3-9c1d-853466cb217b.png

You can then click on the Sign in with Google button to popup the Google Login Modal.

After granting access, you’ll see something like this:

4634f002-9736-4208-bd9f-7b3709a382f6.png

If you refresh the page, it will keep your user logged in as long as Firebase considers the credentials still valid.

Read More Article Visit: Click Here