Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Handing Firebase + Facebook login process

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!

like image 212
KingTim Avatar asked Feb 17 '26 18:02

KingTim


1 Answers

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

like image 67
Jen Jose Avatar answered Feb 20 '26 07:02

Jen Jose



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!