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

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

问题描述

我在我的应用程序中登录了Facebook,我正在圈圈试图平衡需要通过注册屏幕的新用户,以及注册用户应该直接进入应用程序。这是用于处理Facebook登录的功能(当按钮被点击并且Facebook授权时):

  func loginButton(_ loginButton:FBSDKLoginButton !,didComplete结果:FBSDKLoginManagerLoginResult!,错误:错误!){
if error!= nil {
return
}
FBSDKGraphRequest(graphPath:/ me,parameters:[
$ b让accessToken = FBSDKAccessToken.current()
guard让accessTokenString = accessToken?.tokenString else {
让凭证= FIRFacebookAuthProvider.credential(withAccessToken:accessTokenString)

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

if error!= nil {
return
}
if(FBSDKAccessToken.current()!= nil){
//登录
self (与标识符:lo ginToRooms,sender:nil)
} else {
// not logged in
self.performSegue(withIdentifier:showSetupScreen,sender:nil)
}
})

if err!= nil {
return
}
}
}

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



我也在 viewDidLoad

  //当应用程序启动时,如果用户已被授权,请直接带上用户。否则显示登录屏幕。 
FIRAuth.auth()?. addStateDidChangeListener {auth,用户在
中,如果let user = user {
//用户已登录显示主屏幕
self.performSegue(withIdentifier :loginToRooms,发件人:无)
}其他{
//没有用户登录。显示用户登录屏幕
返回
}
}

然而,我在测试中发现,如果我点击Facebook登录并输入Facebook用户名作为新用户,我获得授权,然后直接进入应用程序,无需通过注册屏幕。这会导致各种问题。



对于那些使用Facebook登录Firebase的人来说,处理这种情况的好方法是什么?我需要介绍几种情况:


  1. 如果新用户点击Facebook登录按钮,他们将进入注册屏幕。 / li>
  2. 如果已注册(但已注销)的用户点击Facebook登录按钮,它们将直接进入应用程序。

  3. 登录用户启动应用程序,他们将绕过登录屏幕,并直接进入应用程序。

感谢您的帮助!

解决方案

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

- >做本地FB登录。 (比如你现在正在做的)

- >做firebase身份验证。



- >检查数据是否存在于FirDatabase中

$ p $ F $ C $ F> FBSDKGraphRequest(graphPath:/ me,parameters:[fields:name
$ b让accessToken = FBSDKAccessToken.current()
保护让accessTokenString = accessToken?.tokenString else {return}

$ b $中启动{(connection,result,err) let credentials = FIRFacebookAuthProvider.credential(withAccessToken:accessTokenString)

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

if错误!=无{
返回
}

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



$ b $ if $!$ n $ {
return
}
}

理想情况下,在设置页面之后,您必须将设置信息添加到节点users - > uID下的FirebaseDatabase中。所以检查这个方法是否存在这样的节点。
$ b $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ 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 login-> firbase authenticateion - > Firdatabse节点检查 - > if返回true->登录到应用程序


  1. 如果现有用户注册,则永远不会注销,他们永远不会再看到登录或注册屏幕。


    $ b

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


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

    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.

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

    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.

    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. If a new user taps the Facebook Login button, they will be taken to the signup screen.
    2. If a registered (but logged out) user taps the Facebook Login button, they will be taken straight into the app.
    3. If a registered, still logged-in user launches the app, they will bypass the login screen and be taken straight into the app.

    Thanks for any help!

    解决方案

    When the user clicks on the FB Login button :

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

    -> do the firebase authentication.

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

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

    Test cases :

    1. If a brand new user taps the FB login button on the login screen, they'll be taken to the setup screen.

    Fulfilled.

    1. If an existing, but logged out, user taps the FB login button, they'll bypass the setup screen and go to the first page in the app.

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

    1. If an existing user signs up then never logs out, they'll never see the login or signup screen again.

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

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

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