Search code examples
iosswiftgoogle-signinxserver

XServer.app Google Sing in: Update user data in iOS Swift


I'm building an iOS app and need to allow my users to sign in with Google using XServer

I've downloaded and installed the SDK, created the OAuth credential on my Google Cloud Platform, and this is my code so far:

import GoogleSignIn

class GLogin: UIViewController, GIDSignInDelegate {

    @IBOutlet weak var googleButton: UIButton!
    var googleSignIn = GIDSignIn.sharedInstance()
    

    // Google Sign in button
    @IBAction func gSignInButton(_ sender: Any) {
        googleSignIn?.presentingViewController = self
        googleSignIn?.clientID = "My_Google_OAuth_Id"
        googleSignIn?.delegate = self
        googleSignIn?.signIn()
    }

}


func sign(_ signIn: GIDSignIn!, didSignInFor user: GIDGoogleUser!, withError error: Error!) {
           guard let user = user else {
               print("Google login cancelled.")
               return
           }
           
           // Get User info
           let password = user.userID ?? ""
           let firstName = user.profile.givenName ?? ""
           let lastName = user.profile.familyName ?? ""
           let email = user.profile.email ?? password + "@google.com"
           let profilePicURL = user.profile.imageURL(withDimension: 150)?.absoluteString
           let username = firstName.lowercased() + lastName.lowercased()
           let fullName = firstName + " " + lastName
           
           self.XSSignUp(username: username, password: password, email: email, signInWith: "google") { (results, e) in
           if error == nil {
               
               // I don't know what to do here...

           } else { DispatchQueue.main.async { print(e!) }
       }}// ./ XSSignUp()

    }
       
    func sign(_ signIn: GIDSignIn!, didDisconnectWith user: GIDGoogleUser!, withError error: Error!) {
           // Call your backend server to delete user's info after they requested to delete their account
    }

So, I can successfully retrieve a Google user's data, as well as register the user in the database, but I don't know how to update its full name and profile picture, so those columns are empty after performing the code above.

What am I missing?


Solution

  • I'm the creator of XServer.app, you did a good job so far, so what you're missing is a bit of code to check out if your user already signed up with Google or he's signing in again. You also need to call the XSObject() function to add additional data to your database -> Users table.

    So, in the space where you placed the comment:

    // I don't know what to do here...
    

    Try using this code:

     let resultsArr = results!.components(separatedBy: "-")
     let uID = resultsArr[0]
     let isSignInWithAppleGoogle = resultsArr[1]
    
     // Already signed up
     if isSignInWithAppleGoogle == "true" {
       print("User previously signed up") 
    
     } else {
       DispatchQueue.main.asyncAfter(deadline: .now() + 1, execute: {
    
         // Add data using the XSObject() function
         let params = [
           self.param(columnName: "tableName", value: "Users"),
           self.param(columnName: "ID_id", value: uID),
           self.param(columnName: "FL_profilePic", value: profilePicURL),
           self.param(columnName: "ST_fullname", value: fullName),
         ]
         self.XSObject(params) { (e, obj) in
           if e == nil {
             print("Google user just signed up!")
           // error
           } else { DispatchQueue.main.async { print(e!)
         }}}// ./ XSObject
       })
    
     } // ./ If
    

    Please note that The FL_profilePic and ST_fullname column names must match the names you gave to your columns when you created them in the database.

    For more info, you can check this article too: https://xscoder.hashnode.dev/google-sign-in-with-xserver-ios-swift