Tom
Tom

Reputation: 4033

Firebase Auth Google - always ask for account

I recently implemented a google sign in. It works fine, but there's one thing bothering me.

When the user logs in for the first time, a window is being opened for the user to login in google and choose his account. But when the user logs out and tries to login via google again, somehow his previous login is being remembered and he's just getting logged in.

How can I achieve to ask for google login each time a user chooses to log in via google?

That's how I log the user out:

 try! Auth.auth().signOut()

Login:

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

    print("Successfully logged into Google.")

    guard let authentication = user.authentication else { return }
    let credential = GoogleAuthProvider.credential(withIDToken: authentication.idToken,
                                                   accessToken: authentication.accessToken)

    Auth.auth().signIn(with: credential) { (acc, error) in
        if let error = error {
            print("Failed to create User with Google: ", error)
            return
        }

        let databaseRef = Database.database().reference()
        guard let uid = Auth.auth().currentUser?.uid else { return }
        databaseRef.child("users").observeSingleEvent(of: .value, with: { (snapshot) in

            if snapshot.hasChild("\(uid)") {
                print("Data found.")
                let storyboard = UIStoryboard(name: "Main", bundle: nil)
                let vc = storyboard.instantiateViewController(withIdentifier: "ProfileViewController") as! ProfileViewController
                vc.readLeasing()
                vc.readData()
            } else {
                self.upDataToDatabase(from: user)
            }

        })

        let defaults = UserDefaults.standard

        logInViaGoogle = true
        defaults.set(logInViaGoogle, forKey: "logInViaGoogle")

        print("Successfully created user with Google.")
    }
}

Upvotes: 3

Views: 1354

Answers (1)

Gabriel Machado
Gabriel Machado

Reputation: 493

As poited in Firebase google auth automatically selecting user. How to force it to select account

you can just do

var provider = new firebase.auth.GoogleAuthProvider();
provider.setCustomParameters({
prompt: 'select_account'
});

Upvotes: 2

Related Questions