Authenticate with Firebase using Facebook Login on iOS - Swift 4 - Swift 4 Tutorials W3Schools

Hot

Post Top Ad

27 Sept 2017

Authenticate with Firebase using Facebook Login on iOS - Swift 4

We can authenticate users with Firebase with their Facebook accounts by integrating Facebook Login.

Authenticate with Firebase using Facebook Login on iOS - Swift 4

This Article Covers Following Related questions  :

Using Firebase to Integrate Facebook Login in iOS Apps
Firebase Facebook Login and Authentication Tutorial
Firebase Authentication using facebook login in iOS
Add Facebook authentication to your apps, using Firebase
iOS App using facebook login with firebase auth
Firebase - Facebook Authentication


Before get into this, there are some steps to be done:

  • 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 ID and App Secret you got from Facebook.
    • Then, make sure your OAuth redirect URI (e.g. demo-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.

Enable Facebook Login in firebase console

Facebook Integration:

Firstly we need to integrate Facebook to Xcode project before Authenticate with Firebase.

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

Next initialize the FBSDKLoginButton object, and set delegate to receive login and logout events. For example:
let loginButton = FBSDKLoginButton()
loginButton.delegate = self

Implement delegate method didCompleteWithResult:error:
func loginButton(loginButton: FBSDKLoginButton!, didCompleteWithResult result: FBSDKLoginManagerLoginResult!, error: NSError?) {
  if let error = error {
    print(error.localizedDescription)
    return
  }
  // ... Successfully Logged in
}

Sign In To Firebase:

We need to initialize Firebase for Authentication.

First import the Firebase SDK in AppDelegate.swift file:
import Firebase

Then, in the application:didFinishLaunchingWithOptions: method, initialize the FirebaseApp object:
// Use Firebase library to configure APIs
FirebaseApp.configure()

After a user successfully signs in into Facebook the didCompleteWithResult:error:  method will call, there we get an access token for the signed-in user and exchange it for a Firebase credential:
let credential = FacebookAuthProvider.credential(withAccessToken: FBSDKAccessToken.current().tokenString)

Finally, authenticate with Firebase using the Firebase credential:
Auth.auth().signIn(with: credential) { (user, error) in
    if let error = error {
        // ...
        print(error.localizedDescription)
        return
    }
    // User is signed in
    if let user = user {
        // The user's ID, unique to the Firebase project.
        // Do NOT use this value to authenticate with your backend server,
        // if you have one. Use getTokenWithCompletion:completion: instead.
        let uid = user.uid
        let email = user.email
        let photoURL = user.photoURL
        // ...
    }
}

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.

Sign Out:

In order to sign out a user from Firebase write the following code:
let firebaseAuth = Auth.auth()
do {
    try firebaseAuth.signOut()
} catch let signOutError as NSError {
    print ("Error signing out: %@", signOutError)
}

It will sign Out a user from Firebase and no longer access to Firebase.

Next Steps:


Firebase Authentication using Email & Password

 

Authenticate with Firebase Anonymously on iOS

 

No comments:

Post a Comment

Post Top Ad