Firebase Login - Facebook Sign-in

Authenticate Using Facebook Login on iOS

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

Before you begin

  1. Add Firebase to your iOS project. Include the following pods in your Podfile:
     
         
    pod 'Firebase/Auth'
  2. If you haven't yet connected your app to your Firebase project, do so from the Firebase console.
  3. On the Facebook for Developers site, get the App ID and an App Secret for your app.
  4. Enable Facebook Login:
    1. In the Firebase console, open the Auth section.
    2. On the Sign in method tab, enable the Facebook sign-in method and specify the App IDand App Secret you got from Facebook.
    3. 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

  1. 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:

    SWIFT

    OBJECTIVE-C

     
          
    FBSDKLoginButton * loginButton = [[ FBSDKLoginButton alloc ] init ];
    loginButton
    . delegate = self ;
    In your delegate, implement didCompleteWithResult:error:.

    SWIFT

    OBJECTIVE-C

     
          
    - ( void ) loginButton :( FBSDKLoginButton *) loginButton
        didCompleteWithResult
    :( FBSDKLoginManagerLoginResult *) result
                        error
    :( NSError *) error {
     
    if ( error == nil ) {
       
    // ...
     
    } else {
       
    NSLog ( error . localizedDescription );
     
    }
    }
  2. Import the Firebase module in your UIApplicationDelegate:

    SWIFT

    OBJECTIVE-C

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

    SWIFT

    OBJECTIVE-C

     
          
    // Use Firebase library to configure APIs
    [ FIRApp configure ];
  4. 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:

    SWIFT

    OBJECTIVE-C

     
          
    FIRAuthCredential * credential = [ FIRFacebookAuthProvider
        credentialWithAccessToken
    :[ FBSDKAccessToken currentAccessToken ]. tokenString ];
  5. Finally, authenticate with Firebase using the Firebase credential:

    SWIFT

    OBJECTIVE-C

     
          
    [[ 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:.

SWIFT

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.


你可能感兴趣的:(iOS,firebase)