Building a Reusable React Login Component
Tags: javascript, react, googlelogin, oauth
Published: 2019-07-16 00:16:21

---
title: Building a Reusable React Login Component
published: true
tags: javascript,react,google login, oauth
canonical_url: https://blog.bitsrc.io/building-a-reusable-react-login-component-cc824164ff36
cover_image: https://cdn-images-1.medium.com/max/1024/1*-2Td4b9tjqb5iW4oqClotQ.png
---

#### Learn how to build a reusable and shareable Firebase Google login component


In this chapter, we will continue from where we left off in the [first chapter](https://dev.to/kris/building-a-reusable-firebase-facebook-login-component-27o6)that dealt with implementing a Facebook login and initiating a react app structure. We are going to add Google login to the same app we created in the first chapter. This chapter is short, sweet, and simple because we have already completed the problematic section in the previous chapter. We have broken down this tutorial into various sub-headings for you to grasp the concept about React project, Google login, firebase auth, component implementation, etc. and work accordingly.

Now, let’s get on with implementing the Google login!

### What you’ll learn …?

- How to create a new React app.
- How to pass props in React.
- How to use Firebase Authentication.
- How to create and set up a Google app.
- [How to use Bit.](https://bit.dev)

Most of the points have already been covered in the first chapter, so this chapter makes the concepts even clearer as we move along.

### Problem

As we have done most of the problematic section in the first chapter, this chapter only tackles the issue of implementation of Google login; which is relatively simple compared to Facebook login. So, we need a quick win here without wasting much of our time. We need to generate result quickly, which is an important part that keeps us motivated to reach our end milestones.

### Requirements

Here’s a complete list of plugins, packages, and services we’re going to need to gain something from this tutorial:

- Nodejs v8.x.x or higher installed along with NPM/yarn.
- Firebase and Bit account.
- You’ve already completed the project from the previous chapter.
- Firebase and Bit package.

### Reusable previous code

To make things easier for us, we are going to replicate the Facebook component that we created in the first chapter and change all related names to Google. The project directory structure will look something like below:

![](https://cdn-images-1.medium.com/max/260/1*lCl_4qmH6WU9Xrq7Ugjnkg.png)

The places where we changed the name from Facebook to Google are mentioned in detail below:

1. Folder **FacebookAuth** is changed to **GoogleAuth.**

2. **_FacebookAuth.jsx_** file is changed to **_GoogleAuth.jsx_** file.

3. In **_GoogleAuth.jsx_** file, change the class name to **_GoogleAuth._**

4. In **_handleLogin()_** function, change the **_FacebookAuthProvider_** to **_GoogleAuthProvider._**

Then, we need to change **Facebook** to **Google** in the HTML section as given below in the following code snippet:

```js
<li class="firebaseui-list-item">
     <button
        onClick={this.handleLogin}
        class="firebaseui-idp-button mdl-button mdl-js-button mdl-button--raised firebaseui-idp- **google** firebaseui-id-idp-button">
         <span class="firebaseui-idp-icon-wrapper">

           <img class="firebaseui-idp-icon" alt="" src="https://www.gstatic.com/firebasejs/ui/2.0.0/images/auth/ **google**.svg">
          </span>
          <span class="firebaseui-idp-text firebaseui-idp-text-long">
              Sign in with **Google**
          </span>
          <span class="firebaseui-idp-text firebaseui-idp-text-short">
**Google**
          </span>
      </button>
```

The above code will provide us with the Google login component which we need to import in our _App.js_ file.

Now, we need to copy the code below and add it to our _App.js_ file.

In our _GoogleAuth/index.js_ file, we need to replace all the import of _FacebookAuth_ with _GoogleAuth,_ as shown in the code snippet below:

Now, every change will be made only in our _App.js_ file. We need to change the _App.js_ file to include the component and pass firebase configuration as shown in the code snippet below:

As a result, we will see the following result in our browser screen or app screen:

![](https://cdn-images-1.medium.com/max/417/1*qvDPm_w4Zdw1LJC4U2Lb0w.png)

We can see that this is working fine. Now, we move one to Firebase part of our tutorial chapter.

#### Activating the Sign-in method in Firebase

In this step, we need to activate the Sign-in method in our firebase console for Google login to work. For that, we need to:

Go to firebase console > Authentication > enable Google Sign-in Method

Lastly, we need to provide a project support email and then click on “save” button as shown in the console screenshot below:

![](https://cdn-images-1.medium.com/max/841/1*gouRIMIT2S2z5DzLO6RsMA.png)

After this is done, we need to return to our app and click on Google Login. We will see the following simulation and result in our browser console:

![](https://cdn-images-1.medium.com/max/1024/1*irGP2EddTAJy6dGaD5xsfg.gif)

Finally, we have successfully implemented Google login as well in our react app.

Now, we need to push the code to Bit as in the previous chapter by creating a new collection.

### Make it Reusable and Sharable with Bit

[Share reusable code components as a team · Bit](https://bit.dev)

To make our code reusable and shareable, we’ll do some restructuring and move all our code to ‘components’ (this is not mandatory but a better practice, when using [Bit](https://bit.dev)). Then, we’ll export it (with all its dependencies), to a component collection on [Bit’s cloud](https://bit.dev/components), to be shared with others and easily reused.

In this step, we are going to push the new component i.e. Google login component to Bit again. We already have our Bit package installed and connected to our Bit account. So, we need to create a collection and start by tracking the component.

Note: The first and second step features an installation of Bit account. These steps can be skipped if you already have a Bit account.

#### 1. Create a collection and initialize Bit on a project

Here, we create a new collection on Bit to which we are going to push all our component code. We use the Bit “create collection” configuration page to create a new collection named “firebase-auth-collection” as shown in the screenshot below:

![](https://cdn-images-1.medium.com/max/587/1*FF1zzr2tVDlxytpkbV7Cbw.png)

To successfully push the whole project to Bit, you need to follow the instructions given on the page shown below:

![](https://cdn-images-1.medium.com/max/1024/1*ccQIEhgssTq-cQdb4stu7w.png)

First, we need to install Bit CLI

> Using NPM,

> _npm install bit-bin --global_

Then, we need to go to the project directory in our Bit CLI and perform the commands bit init and run bit login on Bit CLI as shown in the screenshot below:

![](https://cdn-images-1.medium.com/max/715/1*y2bXTuO2sAPDSDdwiJuegw.png)

#### 2. Configure a React compiler for our component

When we configure a compiler we tell Bit to capsule the component with it. Capsuling components together with their compilers gives us the freedom to use, build and test them anywhere. This includes being able to run the code in any of the applications we’d like to use the component in, as well as running it in the cloud to enable features such as the [live component playground](https://bit.dev/davidhu2000/react-spinners/pacman-loader).

```sh
bit import bit.envs/compilers/react --compiler
```

#### 3. Track components

We need to track our component by using the following command in your command prompt or Bit console:

bit add src/components/GoogleAuth

The result after the command is performed is shown below in the screenshot:

![](https://cdn-images-1.medium.com/max/783/1*WXtlxf7cmw6Obha1wZRgSA.png)

#### 4. Tag and export to our collection on Bit’s cloud

Here, we are going to s[et a version](https://docs.bit.dev/docs/versioning-tracked-components.html) to all tracked components, and export to this collection using following bit command:

bit tag --all 1.0.0

![](https://cdn-images-1.medium.com/max/706/1*d_RXXyRRRVTfLYMYUf2Bow.png)

Finally, we need to perform following bit command to push our component code to bit collection that we created before:

```sh
bit export krissnawat.firebase-auth-collection
```

Now, our package is live on [Bit](https://bit.dev/krissnawat/firebase-auth-collection/google-auth). It is our second component in our firebase auth collection as shown in the screenshot below:

![](https://cdn-images-1.medium.com/max/983/1*sMzsErQDmTsRL-EFTMpBMQ.png)

Lastly, we need to replace the component in our _App.js_ file as shown in the code snippet below:

{% gist https://gist.github.com/krissnawat/265a970d969b8f3a4be80a11824868c0 %}

Finally, the implementation of Google login with firebase auth and uploading the component to Bit is complete. Now, we can run the project locally to test everything.

#### Conclusion

In this tutorial, we learned how to implement Google login with firebase. We also learned about implementing the re-usable code in the form of components as in the previous chapter. Finally, we were successful in pushing our second component in our Firebase Auth collection. In the next chapter, we are going to continue with **Twitter Login**.

### Learn More

- [React 16 Lifecycle Methods: How and When to Use Them](https://blog.bitsrc.io/react-16-lifecycle-methods-how-and-when-to-use-them-f4ad31fb2282)
- [How to Share React UI Components between Projects and Apps](https://blog.bitsrc.io/how-to-easily-share-react-components-between-projects-3dd42149c09)
- [12 React UI Layout Grid Components and Libraries for 2019](https://blog.bitsrc.io/12-react-ui-layout-grid-components-and-libraries-for-2019-16e8aa5d0b08)

* * *