首页 文章

为什么我的谷歌登录在我成功登录后没有显示帐户选择?

提问于
浏览
1

我已经在使用Firebase的应用中成功实施了Google登录 . 当我第一次使用谷歌帐户登录时,会出现一个选择谷歌帐户的对话框 .

但是在我成功登录然后退出后,当我再次尝试登录时,选择谷歌帐户的对话框不再出现,它只是直接将我发送到主菜单 .

我想在每次用户通过谷歌登录时始终显示该对话框 . 这是我使用的代码,在我的VC中有谷歌登录按钮

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.
        // ...
    }


}

这是用户点击注销按钮时的代码

@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")
        }
    }

我该怎么办?

2 回答

  • 3

    输入您使用Google登录的视图控制器时尝试注销 . 退出将使当前令牌失效,您将被要求再次使用帐户列表登录 .

    override func viewDidLoad() {
        //Configure and set delegate
        ...
        GIDSignIn.sharedInstance().signOut()
        GIDSignIn.sharedInstance().disconnect()
    }
    
    @IBAction func logOutButtonDidPressed(_ sender: Any) {
        GIDSignIn.sharedInstance().signOut()
        GIDSignIn.sharedInstance().disconnect()
    }
    
  • 0

    单击Google登录按钮时,我们应删除以前的共享实例 .

    @IBAction func googleLoginAction(_ sender: Any) {
                signOut()
                let sighIn:GIDSignIn = GIDSignIn.sharedInstance()
                sighIn.delegate = self;
                sighIn.uiDelegate = self;
                sighIn.shouldFetchBasicProfile = true
                sighIn.scopes = ["https://www.googleapis.com/auth/plus.login","https://www.googleapis.com/auth/userinfo.email","https://www.googleapis.com/auth/userinfo.profile","https://www.googleapis.com/auth/plus.me"];
                sighIn.clientID = ""
                sighIn.signIn()
            }
    
        func signOut() -> Void {
                GIDSignIn.sharedInstance().signOut()
            }
    
    func sign(inWillDispatch signIn: GIDSignIn!, error: Error!) {
    
        }
        func sign(_ signIn: GIDSignIn!, present viewController: UIViewController!) {
            self.present(viewController, animated: true, completion: nil)
        }
        func sign(_ signIn: GIDSignIn!, dismiss viewController: UIViewController!) {
            self.dismiss(animated: true, completion: nil)
        }
    func sign(_ signIn: GIDSignIn!, didSignInFor user: GIDGoogleUser!, withError error: Error!) {
            if (error != nil) {
                return
            }
            reportAuthStatus()
    
            socialId = user.userID
            socialMail = user.profile.email
            socialFirstName = user.profile.givenName
            socialLastName = user.profile.familyName
    
            socialLoginFunction(socialName: "google", socialID: self.socialId)
    
        }
        func sign(_ signIn: GIDSignIn!, didDisconnectWith user: GIDGoogleUser!, withError error: Error!) {
            if (error != nil) {
    
            } else {
    
            }
        }
    

相关问题