Reputation: 101
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
Upvotes: 0
Views: 1540
Reputation: 7057
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)
}
})
}
Upvotes: 1