The segue needs to take place only after the user has been authenticated with their Google account. This authentication process takes place in the AppDelegate. I have a segue between two view controllers rather than a button so when the button is tapped, the segue will only be called for once.
The following code takes place within the sign function in the AppDelegate for anyone who is familiar with Firebase:
Auth.auth().signIn(with: credential) { (authResult, error) in
if let error = error {
print("Firebase sign In error")
print(error)
return
} else {
let db = Firestore.firestore()
db.collection("users").getDocuments() { (querySnapshot, err) in
if let err = err {
print("Error getting documents: \(err)")
} else {
print("Accessing email")
for document in querySnapshot!.documents {
if document.get("uid") as? String != Auth.auth().currentUser?.uid {
db.collection("users").addDocument(data: ["firstName": firstName!, "lastName": lastName!, "email": email!, "uid": authResult!.user.uid]) { (error) in
if error != nil {
print("Error: User data not saved")
return
}
}
}
}
}
}
self.window?.rootViewController!.performSegue(withIdentifier: "googleSegue", sender: nil)
print("User is signed in with Firebase")
}
}
}
However the self.window?.rootViewController!.performSegue(withIdentifier: "googleSegue", sender: nil)
does nothing when it should be occurring and taking the user to the connected view controller after they sign in. The print statement does occur so there is nothing wrong in that regard.
For reference, this is what happens in the viewController file:
@IBAction func googleSignInTapped(_ sender: Any) {
print("User has chosen to sign with Google.")
GIDSignIn.sharedInstance().signIn()
}