You can let your users authenticate with Firebase using their Facebook accounts by integrating Facebook Login into your app.
Before you begin
- Add Firebase to your iOS project. Include the following pods in your
Podfile
: pod 'Firebase/Auth' - If you haven't yet connected your app to your Firebase project, do so from the Firebase console.
- On the Facebook for Developers site, get the App ID and an App Secret for your app.
- Enable Facebook Login:
- In the Firebase console, open the Auth section.
- On the Sign in method tab, enable the Facebook sign-in method and specify the App IDand App Secret you got from Facebook.
- Then, make sure your OAuth redirect URI (e.g.
my-app-12345.firebaseapp.com/__/auth/handler
) is listed as one of your OAuth redirect URIs in your Facebook app's settings page on the Facebook for Developers site in the Product Settings > Facebook Login config.
Authenticate with Firebase
- Integrate Facebook Login into your app by following the developer's documentation. When you initialize the
FBSDKLoginButton
object, set a delegate to receive login and logout events. For example:FBSDKLoginButton * loginButton = [[ FBSDKLoginButton alloc ] init ];In your delegate, implement
loginButton . delegate = self ;didCompleteWithResult:error:
.- ( void ) loginButton :( FBSDKLoginButton *) loginButton
didCompleteWithResult :( FBSDKLoginManagerLoginResult *) result
error :( NSError *) error {
if ( error == nil ) {
// ...
} else {
NSLog ( error . localizedDescription );
}
} - Import the Firebase module in your
UIApplicationDelegate
:@import Firebase ; - Configure a
FirebaseApp
shared instance, typically in your application'sapplication:didFinishLaunchingWithOptions:
method:// Use Firebase library to configure APIs
[ FIRApp configure ]; - After a user successfully signs in, in your implementation of
didCompleteWithResult:error:
, get an access token for the signed-in user and exchange it for a Firebase credential:FIRAuthCredential * credential = [ FIRFacebookAuthProvider
credentialWithAccessToken :[ FBSDKAccessToken currentAccessToken ]. tokenString ]; - Finally, authenticate with Firebase using the Firebase credential:
[[ FIRAuth auth ] signInWithCredential : credential
completion :^( FIRUser * user , NSError * error ) {
if ( error ) {
// ...
return ;
}
// User successfully signed in. Get user data from the FIRUser object
// ...
}];
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
FIRUser
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:
.
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.