Issue
I have successfully implemented a google sign in my app that uses Firebase. when I first sign in using google account a dialog to choose the google account will appear.
but after I successfully login and then log out, when I try to log in again, that dialog to choose google account doesn’t appear again, it just directly send me to the main menu.
I want to always show that dialog every time the user sign in via google. here is the code I use, in my VC that has google sign in button
import UIKit
import Firebase
import GoogleSignIn
import SVProgressHUD
class AuthenticationVC : UIViewController, GIDSignInUIDelegate, GIDSignInDelegate {
var userID : String?
override func viewDidLoad() {
super.viewDidLoad()
// delegate declaration
GIDSignIn.sharedInstance().uiDelegate = self
// For Google SignIn Using Firebase
GIDSignIn.sharedInstance().clientID = FirebaseApp.app()?.options.clientID
GIDSignIn.sharedInstance().delegate = self
}
@IBAction func googleButtonDidPressed(_ sender: Any) {
GIDSignIn.sharedInstance().signIn()
}
}
extension AuthenticationVC {
// MARK: - Firebase Google Sign In Authentication Methods
// The methods below will be triggered if the user want to login via google account
@available(iOS 9.0, *)
func application(_ app: UIApplication, open url: URL, options: [UIApplicationOpenURLOptionsKey : Any] = [:]) -> Bool {
return GIDSignIn.sharedInstance().handle(url,sourceApplication:options[UIApplicationOpenURLOptionsKey.sourceApplication] as? String, annotation: [:])
}
func application(_ application: UIApplication, open url: URL, sourceApplication: String?, annotation: Any) -> Bool {
return GIDSignIn.sharedInstance().handle(url,sourceApplication: sourceApplication,annotation: annotation)
}
func sign(_ signIn: GIDSignIn!, didSignInFor user: GIDGoogleUser!, withError error: Error?) {
SVProgressHUD.show(withStatus: "Harap Tunggu")
if let error = error {
SVProgressHUD.dismiss()
print("failed to login into google")
print("\(error.localizedDescription)")
return
}
print("user successfully signin into google")
guard let authentication = user.authentication else {
SVProgressHUD.dismiss()
return
}
let credential = GoogleAuthProvider.credential(withIDToken: authentication.idToken,
accessToken: authentication.accessToken)
Auth.auth().signIn(with: credential) { (user, error) in
if let error = error {
SVProgressHUD.dismiss()
print("failed to create firebase user using google account")
print("\(error.localizedDescription)")
return
}
print("user successfully logged into firebase")
guard let userKMFromGoogleSignIn = user else {
SVProgressHUD.dismiss()
return
}
self.userID = userKMFromGoogleSignIn.uid
// if the user sign Up for the very first time using google account, then user Basic Information stored in the firestore will be nil, therefore we create user basic data to firestore
// if user has signed up before via google account, then directly send to mainTabBar (it means he/she has picked a city before), otherwise pickCity first
self.checkUserBasicInformationInFirestore(userKMFromGoogleSignIn)
}
}
func sign(_ signIn: GIDSignIn!, didDisconnectWith user: GIDGoogleUser!, withError error: Error!) {
// Perform any operations when the user disconnects from app here.
// ...
}
}
and here is the code when the user taps the logout button
@IBAction func logOutButtonDidPressed(_ sender: Any) {
do {
try Auth.auth().signOut()
print("User Did Log Out")
performSegue(withIdentifier: "Back", sender: nil)
} catch {
showAlert(alertTitle: "Sorry", alertMessage: "\(error)", actionTitle: "Back")
}
}
what should I do?
Solution
Try signing out when entering your view controller. Signing out will expire the current token and you will be asked to sign in again with list of accounts.
override func viewDidLoad() {
//Configure and set delegate
...
GIDSignIn.sharedInstance().signOut()
GIDSignIn.sharedInstance().disconnect()
}
@IBAction func logOutButtonPressed(_ sender: Any) {
GIDSignIn.sharedInstance().signOut()
GIDSignIn.sharedInstance().disconnect()
}
Answered By – badhanganesh
Answer Checked By – Marilyn (BugsFixing Volunteer)