0
votes

I am trying to use google sign in as per the instructions given on the firebase authentication for swift iOS

https://firebase.google.com/docs/auth/ios/google-signin

But following all the instructions still not able to perform the google sign in/ up. When the google sign in button is pressed, it takes to the google authentication page , where I do choose the gmail id/ type and sign in gmail id, but after doing that it doesn't register with the gmail id in firebase.

Below is the code I am using

AppDelegate.swift

 internal func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
        FirebaseApp.configure()

        GIDSignIn.sharedInstance().clientID = FirebaseApp.app()?.options.clientID
        GIDSignIn.sharedInstance().delegate = self

        let authListener = Auth.auth().addStateDidChangeListener { auth, user in
        let storyboard = UIStoryboard(name: "Main", bundle: nil)

        if user != nil {
                //
          let pushManager = PushNotificationManager(userID: Auth.auth().currentUser!.uid)
          pushManager.registerForPushNotifications()
         } else {
             // menu screen
           }
        }
       return true
    }


    @available(iOS 9.0, *)
    func application(_ application: UIApplication, open url: URL, options: [UIApplication.OpenURLOptionsKey : Any])
      -> Bool {
      return GIDSignIn.sharedInstance().handle(url)
    }

    func application(_ application: UIApplication, open url: URL, sourceApplication: String?, annotation: Any) -> Bool {
        return GIDSignIn.sharedInstance().handle(url)
    }

    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)
      // ...
    }

ViewController.Swift

override func viewDidLoad() {
        super.viewDidLoad()
        GIDSignIn.sharedInstance()?.presentingViewController = self
        GIDSignIn.sharedInstance().signIn()
   }

I did implemented GIDSignInDelegate in AppDelegate, Enabled the Google Sign in Firebase Authentication. Created button on storyboard as per the instructions in the above link. I did configured reverse client id as well. Still not able to sign in/up.

Please help. Thanks in Advance.

2
Are you getting any error? - Rob
@Rob i am not getting any error, but its doing nothing, it shows like everything had happened fine, but there is no output as expected - user12937508
You haven't called the sign in yet with Firebase. So currently you're going through Google to authenticate your user. Its the last part on the link you sent: Auth.auth().signIn(with: credential). Calling this code block will handle the actual Firebase sign in. - kobowo
@kobowo okay i just do that right away. thank you - user12937508

2 Answers

0
votes

I think in the delegate method you need to call Auth func.

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
            // ...
        }
    }
0
votes

You're almost done. You just haven't connected the credential you got from Google Sign-in with Firebase Auth. So once you have Credential you can call this code block:

if let unwrappedCredential = credential {
    Auth.auth().signIn(with: unwrappedCredential) { (result, error) in
           //Do something with the result or error
    }
}