Firebase IOS Google Sign in: Change views after successfully signed in

Viewed 645

I'm trying to build my first IOS App and trying to implement Firebase Google sign-in following the documentation located here: https://firebase.google.com/docs/auth/ios/google-signin. The issue is trying to change views if a user has successfully signed in. I have tried several solutions regarding this problem from other Stack overflow posts with none seeming to work. I believe this is because of the new sceneDelegate file which the other solutions don't have to take into account as they're using previous versions of XCode.

The Google sign-in is implemented in the App Delegate and the specific code that actually tries to authenticate a user (In the App delegate) is here:

func sign(_ signIn: GIDSignIn!, didSignInFor user: GIDGoogleUser!, withError error: Error?) {
  // ...
  if let error = error {
    // ...
    return
  }

  guard let authentication = user.authentication else { return }
  let credential = GoogleAuthProvider.credential(withIDToken: authentication.idToken,
                                                    accessToken: authentication.accessToken)

Auth.auth().signIn(with: credential) { (authResult, error) in
  if let error = error {
    // ...
    return
  }
  // User is signed in
  // Here i want to change views
}

}

Once the user is signed in how do I change views? I have tried everything I can find and everything seems to not work or crash the app.

Any help is much appreciated

3 Answers

You can use this in your SignIn function in the AppDelegate:

let nextVC= UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "YourViewController") as! YourViewController
window?.rootViewController = nextVC

Here is an example, how can you do it in your LoginVC:

class LoginViewController: UIViewController {

  override func viewDidLoad() {

    super.viewDidLoad()

    GIDSignIn.sharedInstance().delegate = self

    GIDSignIn.sharedInstance()?.presentingViewController = self

  }
}

This is what I have added in the viewDidLoad method. After that, I have created a custom button which inherits to UIButton. I have created that button programmatically but you can just use @IBOutlet weak var googleB: UIButton!. Then, on that button's tap, you can add this:

@IBAction func googleBTap(_ sender: UIButton) {

    GIDSignIn.sharedInstance().signIn()
}

Then, I have created the extension for GIDSignInDelegate.

extension LoginViewController: GIDSignInDelegate {

 func sign(_ signIn: GIDSignIn!, didSignInFor user: GIDGoogleUser!, withError error: Error?) {

    if let error = error {

        print(error)

        return
    }

    guard let email = user.profile.email else { return }

    guard let authentication = user.authentication else { return }

      let credential = GoogleAuthProvider.credential(withIDToken: authentication.idToken,
                                                accessToken: authentication.accessToken)
  Auth.auth().signIn(with: credential) { (authResult, error) in
    if let error = error {
     print(error)
      return
    }

    //If login is successful then add
   self.navigationController.pushViewController(nextVC(), animated: true) 
   //OR
   self.performSegue() //<-- Whatever goes in here, I don't use these methods as I create views programitacally, but you can use it here and move to next view 
}

 }

}

Well, this is one way, but as your app goes bigger it is better to create a FirebaseHelper class and you are going to use a lot of common methods like fetchProviders, signIn for Google, Facebook, Apple sign-ins. So, you can write one function which can fetch the credential and log in the user, that will be optimized then.

First you set your segue from LoginViewControllerto HomeViewController and set identifier for your segue to loginToHome.

            if error == nil {
                print("User signed in")
                self.performSegue(withIdentifier: "loginToHome", sender: self)
            }
Related