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
- 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
buttonWithLogInCompletion
orlogInWithCompletion
, 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:
Objective-C
@import Firebase;
Swift
import Firebase
- Configure a
FIRApp
shared instance, typically in your application'sapplication:didFinishLaunchingWithOptions:
method:Objective-C
// Use Firebase library to configure APIs [FIRApp configure];
Swift
// 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 = FIRTwitterAuthProvider.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) { // ... }];
Swift
FIRAuth.auth()?.signIn(with: credential) { (user, error) in // ... }
- 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.