Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
242 views
in Technique[技术] by (71.8m points)

ios - 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!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...