You can let your users authenticate with Firebase using their Twitter accounts by integrating Twitter authentication into your app.
Authenticated users can access user-restricted data in Firebase Realtime Database and Cloud Storage.
Before you begin
- Add Firebase to your iOS project. Include the
following pods in your
Podfile:pod 'Firebase/Auth'
- Register your app as a developer application on Twitter and get your app's API Key and API Secret.
- Enable Twitter Login:
- In the Firebase console, open the Auth section.
- On the Sign in method tab, enable the Twitter sign-in method and specify the API Key and API Secret you got from Twitter.
- 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
- Integrate Twitter Login into your app by following the
developer's documentation.
In the callback method of
buttonWithLogInCompletionorlogInWithCompletion, 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 { // ... } }) - Import the Firebase module in your
UIApplicationDelegatesubclass:Swift
import Firebase
Objective-C
@import Firebase;
- Configure a
FIRAppshared instance, typically in your application'sapplication:didFinishLaunchingWithOptions:method:Swift
// Use Firebase library to configure APIs FirebaseApp.configure()
Objective-C
// Use Firebase library to configure APIs [FIRApp configure];
- 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 = TwitterAuthProvider.credential(withToken: session.authToken, secret: session.authTokenSecret)
- Finally, authenticate with Firebase using the Firebase credential:
Objective-C
[[FIRAuth auth] signInWithCredential:credential completion:^(FIRUser *user, NSError *error) { // ... if (error) { // ... return; }Swift
Auth.auth().signIn(with: credential) { (user, error) in // ... if let error = error { // ... return } - 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
updateEmailas in the following example:Objective-C
[[FIRAuth auth].currentUser updateEmail:userInput completion:^(NSError *_Nullable error) { // ... }];Swift
Auth.auth().currentUser?.updateEmail(to: email) { (error) in // ... }
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, you can get the user's basic profile information from the
FIRUserobject. 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
authvariable, 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:.
Swift
let firebaseAuth = Auth.auth()
do {
try firebaseAuth.signOut()
} catch let signOutError as NSError {
print ("Error signing out: %@", signOutError)
}
Objective-C
NSError *signOutError;
BOOL status = [[FIRAuth auth] signOut:&signOutError];
if (!status) {
NSLog(@"Error signing out: %@", signOutError);
return;
}
You may also want to add error handling code for the full range of authentication errors. See Handle Errors.

