Authenticate Using Twitter Login on iOS

You can let your users authenticate with Firebase using their Twitter accounts by integrating Twitter Login into your app.

Authenticate users can access user-restricted data in Firebase Realtime Database and Firebase Storage.

Before you begin

  1. Add Firebase to your iOS project. Include the following pods in your Podfile:
    pod 'Firebase/Auth'
    
  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.

Authenticate with Firebase

  1. Integrate Twitter Login into your app by following the developer's documentation.

    In the callback method of buttonWithLogInCompletion or logInWithCompletion, get the login session's Twitter auth token and Twitter auth token secret. For example:

    Objective-C

     TWTRLogInButton *logInButton =
         [TWTRLogInButton buttonWithLogInCompletion:^(TWTRSession* session,
                                                      NSError* error) {
      if (session) {
        NSString *authToken = session.authToken;
        NSString *authTokenSecret = session.authTokenSecret;
        // ...
      } else {
        // ...
      }
    }];
    

    Swift

    let logInButton = TWTRLogInButton(logInCompletion: { session, error in
      if (session != nil) {
        let authToken = session.authToken
        let authTokenSecret = session.authTokenSecret
        // ...
      } else {
          // ...
      }
    })
    
  2. Import the Firebase module:

    Objective-C

    @import Firebase;
    

    Swift

    import Firebase
    
  3. Configure a FIRApp shared instance, typically in your application's application:didFinishLaunchingWithOptions: method:

    Objective-C

    // Use Firebase library to configure APIs
    [FIRApp configure];
    

    Swift

    // Use Firebase library to configure APIs
    FIRApp.configure()
    
  4. After a user successfully signs in, in your implementation of didCompleteWithResult:error:, exchange the Twitter auth token and Twitter auth token secret for a Firebase credential:

    Objective-C

    FIRAuthCredential *credential =
    [FIRTwitterAuthProvider credentialWithToken:session.authToken
                                         secret:session.authTokenSecret];
    

    Swift

    let credential = FIRTwitterAuthProvider.credential(withToken: session.authToken, secret: session.authTokenSecret)
    
  5. Finally, authenticate with Firebase using the Firebase credential:

    Objective-C

    [[FIRAuth auth] signInWithCredential:credential
                              completion:^(FIRUser *user, NSError *error) {
                                // ...
                              }];
    

    Swift

    FIRAuth.auth()?.signIn(with: credential) { (user, error) in
      // ...
    }
    
  6. Optional: Add an email address to the user's profile. When users sign in to your app with Twitter, their email addresses aren't accessible to Firebase. If you want to add email addresses to the profiles of users that sign in with Twitter, prompt users to provide their email addresses, and then call updateEmail as in the following example:

    Objective-C

    [[FIRAuth auth].currentUser updateEmail:email
                                   callback:^(NSError *error) {
                                     // ...
                                   }];
    

    Swift

    FIRAuth.auth()?.currentUser.updateEmail(email) { (error) in
      if let error = error {
        // ...
      }
    }
    

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, 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, you can get the user's basic profile information from the FIRUser object. See Manage Users.

  • In your Firebase Realtime Database and Firebase 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:.

Objective-C

NSError *error;
[[FIRAuth auth] signOut:&error;];
if (!error) {
  // Sign-out succeeded
}

Swift

try! FIRAuth.auth()!.signOut()

You may also want to add error handling code for the full range of authentication errors. See Handle Errors.

Send feedback about...

Need help? Visit our support page.