Home / Articles / Blog

AuthenticationAPIFrontendBackendFirebase

Twitter Authentication using Firebase

Add twitter login button in frontend using Firebase

BackendShrey Vijayvargiya

Section 1

Authenticating users using Google, Facebook, Twitter and GitHub provide a better user onboarding experience. I have already covered an article for Google authentication, and GitHub authentication using Firebase. Here is the link to the article

text
Github Authentication using Firebase => Article

Overview

  1. Create Twitter Developer app on developer[dor]twitter[dot]com
  2. You need to fill the entire form for Why and What your application does. I am sorry can’t do anything about this.
  3. After getting Dashboard(https://developer.twitter.com/en/portal/dashboard) access, create the app inside the Project and Apps section.
  4. Save the credentials like API key and API secret.
  5. Enable sign-in-method with Twitter on Firebase Console.
  6. Add API credentials in the Firebase Console project with callback URL.
  7. Add Login with Twitter method on Frontend and save access token for the further API call.

You can create your Twitter Developer Apps from this link: https://developer.twitter.com/en/portal/dashboard

After that create a new project from Project & Apps section like I have created.

The left sidebar is where you can create your project app
The left sidebar is where you can create your project app

The left sidebar is where you can create your project app

Access your tokens and secret key from the Keys and Tokens section of your particular project. As seen in the image my project app name is CastTweet and from the Keys and Tokens section, I can regenerate my tokens.

Section 2

Enabling Firebase

Now in Firebase Project Console, go to Authentication -> sign-in-method and enable the Twitter method from the list of methods.

Sign-in-method List
Sign-in-method List

Choose the Twitter method from the list of methods. And add your project app credentials after enabling the sign-in method and lastly copy your callback URL.

Enable Twitter method and add credentials and save your callback URL
Enable Twitter method and add credentials and save your callback URL

The last step is to add that callback URL to the project app inside the developer Twitter portal.

Adding callback URL in Twitter developer project app

Click the Edit button to add a callback URL
Click the Edit button to add a callback URL

Inside the project app click the edit button for Authentication settings, and add your callback URL there which you copied from Firebase Console. 

All the extra processes are done now.

Section 3

Writing Code

We will use the same repository which we use to add GitHub authentication.

Github authentication repository

First, we will create a Firebase provider, which Firebase Twitter Auth requires.

Firebase itself gives a method to create a provider for Twitter authentication.

text
const provider = new firebase.auth.TwitterAuthProvider();

Log in with the Twitter method
Log in with the Twitter method

Our handleTwitterLogin method simply uses the provider we have created.

The Firebase auth method gives the signInWithPopup method which accepts the provider as a parameter and in response, it gives us the access token. The accessToken is the most important token as we need this token in future to make a further API request to GitHub API. In addition, if your user sign-in using GitHub then save this accessToken, because you do need some of the user GitHub credentials which can only be accessed via GitHub API using this accessToken.

Our Access Token in console
Our Access Token in console

Final Login page
Final Login page

Section 4

Conclusion

We can also use Twitter API to authenticate user instead of Firebase but I personally feel this so easy. But if you working in a multinational company, I would strictly recommend using Authentication via Twitter API only.

code repository

Until, next time, Have a good day, People.

Shrey

External Links (10)

Explore topics

More in

Weekly letter

Subscribe to iHateReading

Our once-a-week newsletter on programming, jobs, AI, and building products online.

Our once a week newsletter on Programming, Jobs, AI, and Business