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:
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 :
Fulfilled.
After fb login-> firbase authenticateion ->Firdatabse node check -> if Returns true->login to app
By using your process of cehcking, FIRAuth.auth()?.addStateDidChangeListener {}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With