iOS
...
Users
Sign In With Google
12 min
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 https //www back4app com/docs/ios/parse swift sdk/users/user log in , you can revisit it for more details about the project prerequisites to complete this quickstart, you need xcode an app created at back4app follow the new parse app tutorial to learn how to create a parse app at back4app note follow the install parse sdk (swift) tutorial to create an xcode project connected to back4app 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 once the googlesignin ios option appears in the results, click on the add package button 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 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 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 1 // logincontroller swift file 2 import googlesignin 3 4 5 class logincontroller uiviewcontroller { 6 7 8 private let signinwithgooglebutton uibutton = { 9 let button = uibutton(type system) 10 button setimage(uiimage(named "googleicon"), for normal) 11 button imageview? contentmode = scaleaspectfit 12 return button 13 }() 14 15 override func viewdidload() { 16 super viewdidload() 17 // 18 // layout configuration 19 // 20 21 signinwithgooglebutton addtarget(self, action #selector(handlesigninwithgoogle), for touchupinside) 22 } 23 } 24 25 // mark sign in with google section 26 extension logincontroller { 27 @objc fileprivate func handlesigninwithgoogle() { 28 // todo here we will implement the sign in procedure 29 } 30 } in order to show the sign in form from google, the googlesignin googlesignin sdk allows us to set up and present it via the signin(with\ presenting\ callback) signin(with\ presenting\ callback) method from the gidsignin gidsignin class additionally, we have to pass a gidconfiguration 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 gidgoogleuser object) or an error if the authentication failed 1 // mark sign in with google section 2 extension logincontroller { 3 @objc fileprivate func handlesigninwithgoogle() { 4 let signinconfig = gidconfiguration(clientid "my client id") // see https //developers google com/identity/sign in/ios/sign in for more details 5 6 // method provided by the googlesignin framework see https //developers google com/identity/sign in/ios/sign in for more details 7 gidsignin sharedinstance signin(with signinconfig, presenting self) { \[weak self] googleuser, error in 8 if let error = error { 9 self? showmessage(title "error", message error localizeddescription) 10 return 11 } 12 13 // after google returns a successful sign in, we get the users id and idtoken 14 guard let googleuser = googleuser, 15 let userid = googleuser userid, 16 let idtoken = googleuser authentication idtoken 17 else { fatalerror("this should never happen!?") } 18 19 // todo sign in the user in your back4app application 20 } 21 } 22 } 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 https //www back4app com/docs/ios/parse swift sdk/users/user log in for more details) 1 import parseswift 2 3 struct user parseuser { 4 5 6 var username string? 7 var email string? 8 var emailverified bool? 9 var password string? 10 11 var age int? 12 } therefore, the credential returned by google contains an idtoken idtoken and the user’s id id that will be used to complete the sign in process more precisely, we instantiate a parsegoogle\<user> parsegoogle\<user> object and call the login(id\ idtoken\ completion) login(id\ idtoken\ completion) method 1 // mark sign in with google section 2 extension logincontroller { 3 @objc fileprivate func handlesigninwithgoogle() { 4 gidsignin sharedinstance signout() // this should be called when the user logs out from your app for login testing purposes, we are calling it each time the user taps on the 'signinwithgooglebutton' button 5 6 let signinconfig = gidconfiguration(clientid "my client id") // see https //developers google com/identity/sign in/ios/sign in for more details 7 8 // method provided by the googlesignin framework see https //developers google com/identity/sign in/ios/sign in for more details 9 gidsignin sharedinstance signin(with signinconfig, presenting self) { \[weak self] googleuser, error in 10 if let error = error { 11 self? showmessage(title "error", message error localizeddescription) 12 return 13 } 14 15 // after google returns a successful sign in, we get the users id and idtoken 16 guard let googleuser = googleuser, 17 let userid = googleuser userid, 18 let idtoken = googleuser authentication idtoken 19 else { fatalerror("this should never happen!?") } 20 21 // with the user information returned by google, you need to sign in the user on your back4app application 22 user google login(id userid, idtoken idtoken) { result in 23 // returns the user object asociated to the gidgoogleuser object returned by google 24 switch result { 25 case success(let user) 26 // after the login succeeded, we send the user to the home screen 27 // additionally, you can complete the user information with the data provided by google 28 let homecontroller = homecontroller() 29 homecontroller user = user 30 31 self? navigationcontroller? pushviewcontroller(homecontroller, animated true) 32 case failure(let error) 33 // handle the error if the login process failed 34 self? showmessage(title "failed to sign in", message error message) 35 } 36 } 37 } 38 } 39 } 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 user , containing the google authdata authdata parameters you can also verify that a valid session was created in the dashboard, containing a pointer to the corresponding user user object 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> parsegoogle\<user> object implements the method link(id\ completion ) link(id\ completion ) where you pass the id id of the google account to be linked 1 let googleuserid string // the id of the google account to link to 2 3 user google link(id googleuserid) { \[weak self] result in 4 switch result { 5 case success(let user) 6 // linking succeeded, user object now is linked to the corresponding google account 7 case failure(let error) 8 // linking failed, handle the error 9 } 10 } 5 signing out the sign out process does not vary from the standard way of calling the user signout() 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() user 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 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