Firebase google auth (swift)

我怕爱的太早我们不能终老 提交于 2019-12-11 14:28:30

问题


Following the firebase docs i added the authentication with google account and this is part of the code that i have in my app delegate

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

        if let error = error {
            print("Failed to log into Google: ", error)
            return
        }
            print("Successfully logged into Google", user)
        guard let idToken = user.authentication.idToken else { return }
        guard let accessToken = user.authentication.accessToken else { return }
        let credentials = GoogleAuthProvider.credential(withIDToken: idToken, accessToken: accessToken)

        Auth.auth().signIn(with: credentials, completion: { (user, error) in
            if let err = error {
                print("Failed to create a Firebase User with Google account: ", err)
                return
            }

            guard let uid = user?.uid else { return }

            print("Successfully logged into Firebase with Google", uid)

        })
    }


    func sign(_ signIn: GIDSignIn!, didDisconnectWith user: GIDGoogleUser!, withError error: Error!) {
        // Perform any operations when the user disconnects from app here.
        // ...
    }

but the google button

 fileprivate func setupGoogleButtons() {


        googleButton.frame = CGRect(x: 189, y: 532, width: 118, height: 41)
        view.addSubview(googleButton)

        GIDSignIn.sharedInstance().uiDelegate = self
}

is obviously in a viewController, what i would like to do is an automatically self.performSegue(withIdentifier: "goToHome1", sender: self) as soon as the user logs in with his google account, because at the moment after login the user always finds on the same VC. How can i do this?

UPDATE

I solve my problem following this question Google and Facebook Firebase Auth using swift 3


回答1:


If there is no error then the user signed in successfully so you should segue. The button itself just calls the sign in function. Depending on whether the sign in fails or succeeds you alert or segue.

func sign(_ signIn: GIDSignIn!, didSignInFor user: GIDGoogleUser!, withError error: Error?) {
    if let error = error {
        print("Failed to log into Google: ", error)
        return
    }

    print("Successfully logged into Google", user)
    guard let idToken = user.authentication.idToken else { return }
    guard let accessToken = user.authentication.accessToken else { return }
    let credentials = GoogleAuthProvider.credential(withIDToken: idToken, accessToken: accessToken)

    Auth.auth().signIn(with: credentials, completion: { (user, error) in
        if let err = error {
            print("Failed to create a Firebase User with Google account: ", err)
            return
        }

        guard let uid = user?.uid else { return }
        print("Successfully logged into Firebase with Google", uid)
        // segue here
        DispatchQueue.main.async {
            self.performSegue(withIdentifier: "goToHome1", sender: self)
        }
    })
}



回答2:


The Auth class has a function addAuthStateDidChangeListener:, check it here.

It will trigger any time the user changes, particularly when the user logs in, the callback will return a non-nil user. That's the time you want to perform the segue.



来源:https://stackoverflow.com/questions/47334957/firebase-google-auth-swift

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!