iOS
...
Parse Swift SDK
Users

Sign In With Google

12min

Sign In with Google

Introduction

Nowadays, it is common for applications to offer more than one type of sign-in method. Therefore, for users it is easier to sign in with an account they already have, such as a Google account or Apple ID. Furthermore, social networks (like Facebook) make available an SDK for developers to integrate a sign-in option using their social network credentials.

We start this guide by first integrating the sign-in feature provided by Google. After the user successfully signed in with their Google credential, we use the account information returned by Google to sign in a new user that will be associated with the corresponding Google account.

In this repository, we provide a simple Xcode template where you can test the different sign-in methods we are implementing. This project was already introduced in the previous guide, you can revisit it for more details about the project.

Prerequisites

To complete this quickstart, you need:

Goal

To integrate a user sign-in feature using Google’s SDK and ParseSwift.

1 - Setting up Google’s SDK

Once we have the Xcode project linked to the Back4App application, we proceed to add Google’s SDK which will allow us to implement the sign-in flow. For this example, we use the Swift Package Manager (SPM) to add Google Sign-in dependencies. In the Xcode project, go to File>Add packages… and in the search bar, look for https://github.com/google/GoogleSignIn-iOS.

Document image


Once the GoogleSignIn-iOS option appears in the results, click on the Add Package button.

Document image


Next, go to the dashboard of your Google developer account and create a new project.

Once the project has been created, the next step is to create an OAuth client id. In the Google cloud console select your new project and go to the credentials page. This can be found from the left menu:

Document image


During this process, you may need to set up the OAuth consent screen that will appear in the sign-in with Google form and fill in the corresponding requirements accordingly.

To conclude this step, we have to add in the app’s URL schemes the URL generated during the creation of the OAuth client id. Go to the project configuration and select the main app target. Then go to the info section and add the corresponding URL scheme from your Google project.

Document image


2 - Using Google Sign in with ParseSwift

With GoogleSignIn successfully integrated into your Xcode project, we proceed to implement the sign in with Google feature. In the project example, the LogInController is in charge of handling and displaying the different sign-in options. We then set up the signInWithAppleButton action in this controller:

Swift


In order to show the sign-in form from Google, the GoogleSignIn SDK allows us to set up and present it via the signIn(with:presenting:callback) method from the GIDSignIn class. Additionally, we have to pass a GIDConfiguration object instantiated using the client id created in the previous step, then presenting view controller, and a callback closure. In the callback closure, Google returns the user credentials (embedded in a GIDGoogleUser object) or an error if the authentication failed.

Swift


We then take these credentials and use them to sign in to the user on the Back4App platform. The object representing the user is the following struct (see the previous guide for more details):

Swift


Therefore, the credential returned by Google contains an idToken and the user’s id that will be used to complete the sign-in process. More precisely, we instantiate a ParseGoogle<User> object and call the login(id:idToken:completion) method.

Swift


3 - Verifying user sign in and session creation

To make sure, that the Google sign-in worked, you can look at your Back4App application dashboard and see the new User, containing the Google authData parameters.

Document image


You can also verify that a valid session was created in the dashboard, containing a pointer to the corresponding User object.

Document image


4 - Linking an existing User to a Google account

In case your iOS App requires you to associate a Google account with an existing user in your Back4App platform, the ParseGoogle<User> object implements the method link(id:completion:) where you pass the id of the Google account to be linked:

Swift


5 - Signing out

The sign-out process does not vary from the standard way of calling the User.signout() method (detailed in previous guides). However, when a user signs in with a Google account, for consistency, you should sign out the current Google user as well. We can accomplish this by calling the following method alongside User.signout().

1 GIDSignIn.sharedInstance.signOut()

In order to verify if the current user has a linked Google account, you can check it by looking at the User.current?.authData dictionary.

6 - Run the app

You can go to this repository and download the example project. Before running the project, make sure you set up the provisioning profiles with the ones associated with your developer account.

Conclusion

At the end of this guide, you learned how to sign in or link existing Back4App users on iOS using sign in with Google. In the next guide, we will continue with a different sign-in method.