Swift Firebase Facebook Login Tutorial
In our previous tutorial we learnt how to use the Swift Facebook SDK to login to our app. In this tutorial we will look at how to connect our Facebook login to Firebase! We will be using the source code from the previous tutorial here as a base for this one.
Firebase Facebook Login Setup
Before we can start coding there is some initial setup in Firebase, and the Facebook Developers console.
First of all head on over to the Firebase console. In here create a new app, you will need to have your bundle identifier from your XCode project, ours is “seemu.FirebaseFacebook”. With this setup you will be provided with a GoogleService-info.plist file. Download this and add it to your project, ensure to add it to all your targets!
Now go over to the Facebook Developer Console. Go into your app, under Settings select Basic. You will need the App ID and App Secret for the next step.
Now in your Firebase project console, select Authentication. Enable Facebook and paste your App ID and App secret. Then you will see a redirect URL, copy this, we need to place it in the Facebook developers console.
In your Facebook developer console, go back to your app. Under products select Facebook Login, settings. You will see a “Valid OAuth Redirect URIs”, paste the one from Firebase here.
Now we are setup, lets get back to our app coding! Recall that this tutorial is based off the Facebook Login one, so grab the source code here if you don’t have it already!
Coding the Firebase Facebook Login
First off all open the existing podfile, and add the following to it:
pod ‘Firebase/Auth’.
Run pod install from terminal in the folder directory, once it is installed open up your project again.
In the AppDelegate.swift file add the following import up the top
import FirebaseCore
Next in the application didFinishLaunchingWithOptions function add the following line of code to configure Firebase.
FirebaseApp.configure()
Then go to the ViewController.swift class and add
import FirebaseAuth
Next up add the following function
func firebaseFacebookLogin(accessToken: String) { let credential = FacebookAuthProvider.credential(withAccessToken: accessToken) Auth.auth().signInAndRetrieveData(with: credential) { (authResult, error) in if let error = error { print("Firebase Login Error") print(error) return } // User is signed in print("Firebase Login Done") print(authResult) if let user = Auth.auth().currentUser { print("Current Firebase user is ", user) print(user) } } }
Now add the following line of code to the end of viewDidLoad and the loginButtonDidCompleteLogin on the .success. Once the user is logs in with Facebook it will then trigger the Firebase login.
firebaseFacebookLogin(accessToken: accessToken.authenticationToken)
Once you are logged in with Facebook you will see the current user in the console as follows.
Firebase Login Done
Optional(<FIRAuthDataResult: 0x0232434>)
Current firebase user is
<FIRUser: 0x60000435f323>
Now on your Firebase console go to Develop->Authentication. You will see your Facebook Firebase user you logged in with as follows!