Create a Reusable OAuth React Component with Oauth.io and Bit
Tags: webdev, react, javascript
Published: 2019-01-22 16:09:51

---
title: Create a Reusable OAuth React Component with Oauth.io and Bit
published: true
tags: web-development,react,javascript,reactjs
canonical_url: https://blog.bitsrc.io/create-a-reusable-oauth-react-component-with-oauth-io-and-bit-72cd2d6dbee7
---

#### Create the component once, share it with Bit, use it anywhere to save time!

![](https://cdn-images-1.medium.com/max/1024/1*iiMYBk-3F4jW52sncb9WsA.png)

Who doesn’t want to focus on creating product over dealing with OAuth logic in the code? Reduce the OAuth footprint in your code to a single request!

In this post, we will create a simple reusable OAuth component in React with OAuth.io, the OAuth market player with over 100 providers! Let’s see how.

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

#### Setup OAuth.io User

Register yourself at Oauth.io, it’s free :)

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

Upon login, you’ll get to the dashboard as shown below.

![](https://cdn-images-1.medium.com/max/1024/1*gnKQjLS0NnZ-3hIEaL8X1A.png)

You can then try any integration, but remenber to setup the Integrated API first! Here’s how.

### Add API

Click on Add APIs.

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

Select Facebook as a provider to the app.

![](https://cdn-images-1.medium.com/max/605/1*9VX-OCgYs1x1ODen8VukFw.png)

Now, you need client\_id and client\_secret. So, create a Facebook app to get those credentials.

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

Go to [https://developers.facebook.com](https://developers.facebook.com) to create an app and insert oauth.io as the domain for the app.

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

Add callback URI as oauth.io/auth

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

Put the id and secret in the form at OAuth.io.

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

Hit save, you can also add more APIs. Let’s make use of Facebook API first.

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

### Create a React Component

Next, let’s create a React Component to use Oauth.io SDK. The documentation for oauth.io is located at [http://docs.oauth.io](http://docs.oauth.io/)

For a quick demonstration, I’ll prepare a small component using create-react-app in VS Code. It will look like this.

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

Here’s the OAuth component structure.

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

### Make the component reusable with Bit

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

Bit is cloud-based software composition tool.

It’s a platform that lets you easily share, organize, discover and use components to build multiple applications. A team can create a collection of their shared components, which they can share, update and sync across multiple projects and apps.

Simply put, you can think of Bit as your component “Lego box” to which you can share components from any project, and then find and use them to build more things. Useful, right? there’s much more, but we won’t get into it now.

[Bit - Share and build with code components](https://bitsrc.io)

**Why did I put a section about Bit in the OAuth demonstration?**

I’m using Bit as part of my component workflow, which I use to make my components reusable and consume them in my different apps. This component also became a part of my collection, as you will now see.

First, login to Bit and create your collection (Scope).

![](https://cdn-images-1.medium.com/max/1024/1*NbsjniwoRi7YnR4d8MsKHg.png)<figcaption>create bit scope</figcaption>

Fill in the required details.

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

I named it react-o-auth and kept it public.

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

Now our collection is ready; let’s use Bit to quickly share the component from the repository it’s in (Bit does the “magic”; it automatically isolates the component and shares it to the cloud, without a single code or file change!).

Let’s share the component.

### Install Bit

If you don’t have Bit already, install it globally using

```
npm install -g bit-bin
```

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

### Initialize Bit for the project

Initialize Bit workspace with:

```
bit init
```

This command adds bit.json and .bitmap files so Bit can start tracking and isolating components in the repository when told to do so.

#### Connect to your collection

Use bit login to login to Bit. The link will open in the browser.

![](https://cdn-images-1.medium.com/max/565/1*Lchn1fYH-VnsC6C45jhUTw.png)

Authentication success is reflected in the terminal as well.

![](https://cdn-images-1.medium.com/max/705/1*26fPjMFap6HKIiBBb9jFew.png)

### Start to developing

Open facebook.jsx and follow step by step

1. import necessary libraries

```
import React from "react";
import { OAuth } from "oauthio-web";
```

2. create _FacebookAuth_ class

```
export default class FacebookAuth extends React.Component {

}
```

3. Create a state to store access token

```
state = {

access\_token: ""

};
```

4. Initialize OAuth-io SDK withinitialize and receive data from props

```
componentDidMount() {
   OAuth.initialize(this.props.api\_key);
}
```

5. Create an OAuth handler method

```
Auth = () => {
    OAuth.popup("facebook")
      .done(res => {
          this.setState({ access\_token: res.access\_token });
      })
      .fail(err => {
    });
};
```

6. Render a login button

```
render() {
   return (
      <button onClick={this.Auth}
       className="btn btn-tw btn-block">
       Sign in with facebook
      </button>
   );
}
```

7. Lastly, import this component in index.js

```
import FacebookAuth from "./components/FacebookAuth";
```

8. Activate the component by placing it somewhere

```
<FacebookAuth api\_key={"Fcdn9FAU7dhM0TztVA28NM"} />
```

### Add the components to Bit

Now, ask Bit to track all the components you put in src/components directory.

```
bit add src/components/\*
```

The result

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

Run bit status to see the current status of yout Bit instance.

![](https://cdn-images-1.medium.com/max/708/1*0tdEx01Cs5aTT9wwIDOdtg.png)

[Set a Bit compiler](https://bitsrc.io/bit/envs) for the workspace (I’ll use react) :

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

and you can build your own components.

```
bit build components/o-auth
```

Now, declare this as the 1.0.0 version.

```
bit tag --all 1.0.0
```

Run bit status again to confirm this version is attached:

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

Push them to the Bit remote scope:

```
bit export krissnawat.react-o-auth
```

That’s it! The component you just exported will now show in your collection.

Any comment-block above the component definition is used as the component description, and if I were to add tests- Bit would run them too.

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

Now, you can easily Install any of those components in a separate project using npm. Just add the Bit registry to the npm config:

```
npm config set '@bit:registry' [https://node.bitsrc.io](https://node.bitsrc.io)
```

Get a Bit component with this command

```
npm i @bit/krissnawat.react-o-auth.components.o-auth
```

### Wrapping up

That’s it for this tutorial!

We created a reusable React OAuth component that renders a simple Facebook login option using OAuth.io and shared the reusable component with Bit to easily reuse it in other projects.

Hope you had a good time, feel free to comment and ask anything. Show some support by clapping!

### Learn more

- [5 Tools for Faster Development in React](https://blog.bitsrc.io/5-tools-for-faster-development-in-react-676f134050f2)
- [How to Share React UI Components between Projects and Apps](https://blog.bitsrc.io/how-to-easily-share-react-components-between-projects-3dd42149c09)
- [11 React UI Component Libraries you Should Know in 2019](https://blog.bitsrc.io/11-react-component-libraries-you-should-know-178eb1dd6aa4)

* * *