Swift Firebase Google Login Tutorial
In our previous tutorial we learnt how to use the Swift Google SDK to login to our app. In this tutorial we will look at how to connect our Google login to Firebase! We will be using the source codefrom the previous tutorial here as a base for this one.
Firebase Google Login Setup
Before we can start coding there is some inital setup in the Firebase console and within our XCode project. Before this, lets take a look at how the login will actually work:
- Our iOS App logs in to Google with the Google SDK
- Google returns a unique token which is used to authenticate and identify our account for all future actions with Google
- Our iOS App logs into Firebase and passes the Google Token we have
- Firebase returns a unique token which is used to authenticate and identify our account for all future actions with Firebase
With this out of the way head on over to the Firebase console. n here create a new app, you will need to have your bundle identifier from your XCode project, ours is “seemu.FirebaseGoogle”. 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!
XCode project and code setup
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.
Now we are in our project we need to update the URL whitelist to include the new Firebase Login one, head on over to the project target and select the ‘Info’ tab. Add a new URL type, copy the REVERSED_CLIENT_ID from the GoogleService-info.plist to add it
Now time for the code!
In the AppDelegate.swift file add the following import up the top
import FirebaseCore
Then under applicationDidFinishLaunching with options add the Firebase configuration to the top line:
FirebaseApp.configure()
Then change the GoogleClientID to the following, this will make it use the Firebase one instead as it will use a different GoogleClientID.
GIDSignIn.sharedInstance().clientID = FirebaseApp.app()?.options.clientID
Next up add the end of our current signIn function for Google add the following to login with Firebase:
// Firebase sign in guard let authentication = user.authentication else { return } let credential = GoogleAuthProvider.credential(withIDToken: authentication.idToken, accessToken: authentication.accessToken) print("Sign in with Firebase") Auth.auth().signInAndRetrieveData(with: credential) { (authResult, error) in if let error = error { // ... print("Firebase sign in error") print(error) return } print("User is signed in with firebase") // User is signed in // ... }
This will attempt to login in to Firebase after logging in with Google. If it fails it will print out the error to the console, otherwise it will print our the User is signed in to Firebase to the console.
If the user is signed in with Firebase all further Firebase funciton calls will also have the current user’s details sent.
Finally to let the user be able to log out of Firebase go on over to the ViewController.swift and add the following code to our sign out function so Firebase will also logout!
// Firebase sign out let firebaseAuth = Auth.auth() do { try firebaseAuth.signOut() } catch let signOutError as NSError { print ("Error signing out: %@", signOutError) } print("Signed out")
Volla! You have now setup our app to login to Firebase with a Google account