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:
- If a new user taps the Facebook Login button, they will be taken to the signup screen.
- If a registered (but logged out) user taps the Facebook Login button, they will be taken straight into the app.
- 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