Authenticate Using Twitter in JavaScript

You can let your users authenticate with Firebase using their Twitter accounts by integrating Twitter authentication into your app. You can integrate Twitter authentication either by using the Firebase SDK to carry out the sign-in flow, or by carrying out the Twitter OAuth flow manually and passing the resulting access token and secret to Firebase.

Before you begin

  1. Add Firebase to your JavaScript project.
  2. Register your app as a developer application on Twitter and get your app's API Key and API Secret.
  3. Enable Twitter Login:
    1. In the Firebase console, open the Auth section.
    2. On the Sign in method tab, enable the Twitter sign-in method and specify the API Key and API Secret you got from Twitter.
    3. Then, make sure your Firebase OAuth redirect URI (e.g. my-app-12345.firebaseapp.com/__/auth/handler) is set as your Callback URL in your app's settings page on your Twitter app's config.

Handle the sign-in flow with the Firebase SDK

If you are building a web app, the easiest way to authenticate your users with Firebase using their Twitter accounts is to handle the sign-in flow with the Firebase JavaScript SDK. (If you want to authenticate a user in Node.js or other non-browser environment, you must handle the sign-in flow manually.)

To handle the sign-in flow with the Firebase JavaScript SDK, follow these steps:

  1. Create an instance of the Twitter provider object:
    var provider = new firebase.auth.TwitterAuthProvider();
    
  2. Optional: To localize the provider's OAuth flow to the user's preferred language without explicitly passing the relevant custom OAuth parameters, update the language code on the Auth instance before starting the OAuth flow. For example:
    firebase.auth().languageCode = 'pt';
    // To apply the default browser preference instead of explicitly setting it.
    // firebase.auth().useDeviceLanguage();
    
  3. Optional: Specify additional custom OAuth provider parameters that you want to send with the OAuth request. To add a custom parameter, call setCustomParameters on the initialized provider with an object containing the key as specified by the OAuth provider documentation and the corresponding value. For example:
    provider.setCustomParameters({
      'lang': 'es'
    });
    
    Reserved required OAuth parameters are not allowed and will be ignored. See the authentication provider reference for more details.
  4. Authenticate with Firebase using the Twitter provider object. You can prompt your users to sign in with their Twitter accounts either by opening a pop-up window or by redirecting to the sign-in page. The redirect method is preferred on mobile devices.
    • To sign in with a pop-up window, call signInWithPopup:
      firebase.auth().signInWithPopup(provider).then(function(result) {
        // This gives you a the Twitter OAuth 1.0 Access Token and Secret.
        // You can use these server side with your app's credentials to access the Twitter API.
        var token = result.credential.accessToken;
        var secret = result.credential.secret;
        // The signed-in user info.
        var user = result.user;
        // ...
      }).catch(function(error) {
        // Handle Errors here.
        var errorCode = error.code;
        var errorMessage = error.message;
        // The email of the user's account used.
        var email = error.email;
        // The firebase.auth.AuthCredential type that was used.
        var credential = error.credential;
        // ...
      });
      
      Also notice that you can retrieve the Twitter provider's OAuth token which can be used to fetch additional data using the Twitter APIs.

      This is also where you can catch and handle errors. For a list of error codes have a look at the Auth Reference Docs.

    • To sign in by redirecting to the sign-in page, call signInWithRedirect:
      firebase.auth().signInWithRedirect(provider);
      
      Then, you can also retrieve the Twitter provider's OAuth token by calling getRedirectResult when your page loads:
      firebase.auth().getRedirectResult().then(function(result) {
        if (result.credential) {
          // This gives you a the Twitter OAuth 1.0 Access Token and Secret.
          // You can use these server side with your app's credentials to access the Twitter API.
          var token = result.credential.accessToken;
          var secret = result.credential.secret;
          // ...
        }
        // The signed-in user info.
        var user = result.user;
      }).catch(function(error) {
        // Handle Errors here.
        var errorCode = error.code;
        var errorMessage = error.message;
        // The email of the user's account used.
        var email = error.email;
        // The firebase.auth.AuthCredential type that was used.
        var credential = error.credential;
        // ...
      });
      
      This is also where you can catch and handle errors. For a list of error codes have a look at the Auth Reference Docs.

Handle the sign-in flow manually

You can also authenticate with Firebase using a Twitter account by handling the sign-in flow by calling the Twitter OAuth endpoints:

  1. Integrate Twitter authentication into your app by following the developer's documentation. At the end of the Twitter sign-in flow, you will receive an OAuth access token and an OAuth secret.
  2. If you need to sign in on a Node.js application, send the OAuth access token and the OAuth secret to the Node.js application.
  3. After a user successfully signs in with Twitter, exchange the OAuth access token and OAuth secret for a Firebase credential:
    var credential = firebase.auth.TwitterAuthProvider.credential(token, secret);
    
  4. Authenticate with Firebase using the Firebase credential:
    firebase.auth().signInWithCredential(credential).catch(function(error) {
      // Handle Errors here.
      var errorCode = error.code;
      var errorMessage = error.message;
      // The email of the user's account used.
      var email = error.email;
      // The firebase.auth.AuthCredential type that was used.
      var credential = error.credential;
      // ...
    });

Authenticate with Firebase in a Chrome extension

If you are building a Chrome extension app, you must whitelist your Chrome extension ID:

  1. Open your project in the Firebase console.
  2. In the Authentication section, open the Sign-in method page.
  3. Add a URI like the following to the list of OAuth redirect domains:
    chrome-extension://CHROME_EXTENSION_ID

Only popup operations (signInWithPopup and linkWithPopup) are available to Chrome extensions, as Chrome extensions cannot use HTTP redirects. You should call these methods from a background script rather than a browser action popup, as the authentication popup will cancel the browser action popup.

Next steps

After a user signs in for the first time, a new user account is created and linked to the credentials—that is, the user name and password, phone number, or auth provider information—the user signed in with. This new account is stored as part of your Firebase project, and can be used to identify a user across every app in your project, regardless of how the user signs in.

  • In your apps, the recommended way to know the auth status of your user is to set an observer on the Auth object. You can then get the user's basic profile information from the User object. See Manage Users.

  • In your Firebase Realtime Database and Cloud Storage Security Rules, you can get the signed-in user's unique user ID from the auth variable, and use it to control what data a user can access.

You can allow users to sign in to your app using multiple authentication providers by linking auth provider credentials to an existing user account.

To sign out a user, call signOut:

firebase.auth().signOut().then(function() {
  // Sign-out successful.
}).catch(function(error) {
  // An error happened.
});

Send feedback about...

Need help? Visit our support page.