A True Novice A True Novice - 1 year ago 264
Swift Question

Segue After Google Sign In

I can't get my segue to work after google log in. For starters, I'm using SwiftKeychainWrapper to store the user information. I've used it for facebook and it works but, now that I'm setting up the Google LogIn, it breaks. I've tried several different approaches: Putting the delegate into the appDelegate but, that doesn't allow for the segue, putting the delegate into the login VC, and it just doesn't move.

Could someone help!!!!



import UIKit
import Firebase
import SwiftKeychainWrapper
import FBSDKCoreKit
import FBSDKLoginKit
import GoogleSignIn

class SignIn: UIViewController, GIDSignInUIDelegate, GIDSignInDelegate, UITextFieldDelegate {



@IBOutlet weak var emailField: UITextField!
@IBOutlet weak var passwordField: UITextField!

override func viewDidLoad() {
super.viewDidLoad()

self.emailField.delegate = self
self.passwordField.delegate = self

GIDSignIn.sharedInstance().uiDelegate = self
GIDSignIn.sharedInstance().signInSilently()
GIDSignIn.sharedInstance().clientID = FirebaseApp.app()!.options.clientID
GIDSignIn.sharedInstance().delegate = self

}

override func viewDidAppear(_ animated: Bool) {
if let _ = KeychainWrapper.standard.string(forKey: KEY_UID){
print("NOTE: ID found in keychain")
performSegue(withIdentifier: "goToFeed", sender: nil)
}
}

func sign(_ signIn: GIDSignIn!, didSignInFor user: GIDGoogleUser!, withError error: Error!) {
if (GIDSignIn.sharedInstance().hasAuthInKeychain()){
print("signed in")
performSegue(withIdentifier: "goToFeed", sender: self)
}
}

func googleBtnTapped(_ sender: Any) {

if let authentication = user.authentication
{
let credential = GoogleAuthProvider.credential(withIDToken: authentication.idToken, accessToken: authentication.accessToken)

Auth.auth()?.signIn(with: credential, completion: { (user, error) -> Void in
if error != nil
{
print("Problem at signing in with google with error : \(error)")

}
else if error == nil{
print("user successfully signed in through GOOGLE! uid:\(FIRAuth.auth()!.currentUser!.uid)")

}
})
}
}


@IBAction func facebookBtnTapped(_ sender: AnyObject) {

let facebookLogin = FBSDKLoginManager()

facebookLogin.logIn(withReadPermissions: ["email"], from: self) { (result, error) in
if error != nil {
print("NOTE: Unable to authenticate with Facebook - \(String(describing: error))")
} else if result?.isCancelled == true {
print("NOTE: User cancelled Facebook authentication")
} else {
print("NOTE: Successfully authenticated with Facebook")
let credential = FacebookAuthProvider.credential(withAccessToken: FBSDKAccessToken.current().tokenString)
self.firebaseAuth(credential)
}
}

}





I've been banging my head for days now and I'm looking for any kinds of assistance at this point. Thanks.

Answer Source

Just call GIDSignIn.sharedInstance().signIn() in your googleBtnTapped function like this:

func googleBtnTapped(_ sender: Any) {
    GIDSignIn.sharedInstance().signIn()
}

and then put the authentication code inside the sign function like this:

func sign(_ signIn: GIDSignIn!, didSignInFor user: GIDGoogleUser!, withError error: Error!) {
    if let authentication = user.authentication {
        let credential = GoogleAuthProvider.credential(withIDToken: authentication.idToken, accessToken: authentication.accessToken)

        Auth.auth()?.signIn(with: credential, completion: { (user, error) -> Void in
            if error != nil {
                print("Problem at signing in with google with error : \(error)")
            } else if error == nil {
                print("user successfully signed in through GOOGLE! uid:\(FIRAuth.auth()!.currentUser!.uid)")
                print("signed in")
                performSegue(withIdentifier: "goToFeed", sender: self)
            }
        })
    }
}
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download