Firebase Google Auth(Swift) [英] Firebase google auth (swift)

查看:67
本文介绍了Firebase Google Auth(Swift)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在firebase文档之后,我添加了带有google帐户的身份验证,这是我在我的应用程序委托中拥有的代码的一部分

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

但使用Google按钮

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
}

显然是在viewController中,我想做的是,只要用户使用自己的google帐户登录,我就会自动执行 self.performSegue(withIdentifier:"goToHome1",发件人:self),因为在登录后,用户始终会在同一VC上找到.我怎样才能做到这一点?

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?

更新

我按照以下问题使用Google和Facebook Firebase Auth解决了我的问题迅速3

推荐答案

如果没有错误,则用户已成功登录,因此您应该进行安全检查.该按钮本身仅调用登录功能.取决于登录是成功还是失败,您会发出警报还是警惕.

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

这篇关于Firebase Google Auth(Swift)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆