处理 Firebase + Facebook 登录流程 [英] Handing Firebase + Facebook login process

查看:61
本文介绍了处理 Firebase + Facebook 登录流程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的应用中有 Facebook 登录功能,我正在绕圈子跑,试图在需要通过注册屏幕的新用户和应该直接进入应用的注册用户之间取得平衡.这是处理 Facebook 登录的函数(当点击按钮并且 Facebook 授权时):

I have Facebook Login in my app, and I'm running in circles trying to get a balance to new users needing to go through the signup screen, and registered users who should just be taken straight into the app. This is the function for handling Facebook Login (when the button is tapped and Facebook authorizes):

func loginButton(_ loginButton: FBSDKLoginButton!, didCompleteWith result: FBSDKLoginManagerLoginResult!, error: Error!) {
    if error != nil {
        return
    }
    FBSDKGraphRequest(graphPath: "/me", parameters: ["fields": "name"]).start { (connection, result, err) in

        let accessToken = FBSDKAccessToken.current()
        guard let accessTokenString = accessToken?.tokenString else {return}
        let credentials = FIRFacebookAuthProvider.credential(withAccessToken: accessTokenString)

        FIRAuth.auth()?.signIn(with: credentials, completion: { (user, error) in

            if error != nil {
                return
            }
            if(FBSDKAccessToken.current() != nil){
                // logged in
                self.performSegue(withIdentifier: "loginToRooms", sender: nil)
            }else{
                // not logged in
                self.performSegue(withIdentifier: "showSetupScreen", sender: nil)
            }
        })

        if err != nil {
            return
        }
    }
}

在上面的 FIRAuth.auth 块中,我基本上说一旦 Facebook 登录按钮被点击并通过 Facebook 授权,如果用户有访问令牌,直接进入应用程序.否则,请转到用户将在其中输入必要信息的注册屏幕.

In the FIRAuth.auth block above, I basically say that once the Facebook Login button is tapped and goes through the Facebook authorization, if the user has an access token, go straight into the app. Otherwise, go to the sign-up screen where the user will enter the necessary information.

我在 viewDidLoad 中也有这个,所以当应用程序启动时,如果用户之前登录过,他们甚至不会看到登录屏幕,他们会直接进入应用:

I also have this in viewDidLoad, so when the app is launched, if the user was previously logged in, they won't even see the login screen, they'll just go straight into the app:

    // When app is launched, bring user straight in if they're already authorized. Otherwise show the login screen.
    FIRAuth.auth()?.addStateDidChangeListener { auth, user in
        if let user = user {
            // User is signed in. Show home screen
            self.performSegue(withIdentifier: "loginToRooms", sender: nil)
        } else {
            // No User is signed in. Show user the login screen
            return
        }
    }

但是,我在测试过程中发现,如果我点按 Facebook 登录并以新用户身份输入 Facebook 凭据,我将获得授权,然后直接进入应用程序,而无需通过注册屏幕.这会导致各种问题.

However I've found during testing that if I tap the Facebook Login and enter Facebook credentials as a new user, I get authorized and then sent straight into the app, without going through the signup screen. This causes all sorts of problems.

对于那些将 Facebook 登录与 Firebase 结合使用的人来说,处理这种情况的好方法是什么?我需要涵盖几个场景:

For those of you who use Facebook Login with Firebase, what's a good way to handle this situation? I need to cover a few scenarios:

  1. 如果新用户点击 Facebook 登录按钮,他们将被带到注册屏幕.
  2. 如果已注册(但已退出)的用户点击 Facebook 登录按钮,他们将直接进入该应用.
  3. 如果已注册且仍处于登录状态的用户启动应用程序,他们将绕过登录屏幕并直接进入应用程序.

感谢您的帮助!

推荐答案

当用户点击FB登录按钮时:

When the user clicks on the FB Login button :

-> 做原生FB登录.(比如你现在在做什么)

-> Do the native FB login . (like what you are doing now)

-> 进行 Firebase 身份验证.

-> do the firebase authentication.

-> 但是另外,检查FirDatabase中是否存在Data

-> But additionally, check whether the Data exists in the FirDatabase

      FBSDKGraphRequest(graphPath: "/me", parameters: ["fields": "name"]).start { (connection, result, err) in

            let accessToken = FBSDKAccessToken.current()
            guard let accessTokenString = accessToken?.tokenString else {return}
            let credentials = FIRFacebookAuthProvider.credential(withAccessToken: accessTokenString)

            FIRAuth.auth()?.signIn(with: credentials, completion: { (user, error) in

                if error != nil {
                    return
                }

                self.checkDataExistsinfirDataBaaseForUID(user.uid){
                  loginStaus in
                  if(loginStaus){
                        // logged in
                    self.performSegue(withIdentifier: "loginToRooms", sender: nil)
                   }else{
                        // not logged in
                    self.performSegue(withIdentifier: "showSetupScreen", sender: nil)
                   }
}

            })

            if err != nil {
                return
            }
        }

理想情况下,在设置页面之后,您必须在节点 users -> uID 下的 FirebaseDatabase 中添加设置信息.所以检查这个方法中是否存在这样的节点.

Ideally, after the setup page you must be adding the set-up information in the FirebaseDatabase under the node users -> uID. So check whether any such node is present in this method.

func checkDataExistsinfirDataBaaseForUID(_ uid: String, completion: (result: Bool) -> Void) {
 let ref = FIRDatabase.database().reference().child("users").child(uid)

    ref.observeSingleEventOfType(.Value, withBlock: { (snapshot) in
        completion(snapshot.exists())
     })
}

测试用例:

  1. 如果新用户点击登录屏幕上的 FB 登录按钮,他们将被带到设置屏幕.

完成.

  1. 如果现有但已注销的用户点击 FB 登录按钮,他们将绕过设置屏幕并转到应用的第一页.

fb登录后-> firbase认证->Firdatabse节点检查->如果返回true->登录应用程序

After fb login-> firbase authenticateion ->Firdatabse node check -> if Returns true->login to app

  1. 如果现有用户注册后永远不会退出,他们将永远不会再看到登录或注册屏幕.

通过使用您的校验过程,FIRAuth.auth()?.addStateDidChangeListener {}

By using your process of cehcking, FIRAuth.auth()?.addStateDidChangeListener {}

这篇关于处理 Firebase + Facebook 登录流程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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