Facebook Login screen showing twice, the first one is the regular login page, but the second says "you have already authorized my app name". Can anyone tell me what I'm doing wrong in my code.
Here is my login code:
static func createAndLogin(_ viewController: UIViewController, completion: @escaping (_ success: Bool) -> Void) {
let loginManager = FBSDKLoginManager()
loginManager.logOut()
loginManager.logIn(withReadPermissions: ["public_profile", "email", "user_friends"], from: viewController) { (result, error) -> Void in
if error != nil {
print("login FAILED \(error)")
completion(false)
} else if (result?.isCancelled)!{
print("login is CANCELLED")
completion(false)
} else if FBSDKAccessToken.current() != nil {
let accessToken = FBSDKAccessToken.current().tokenString
let credential = FIRFacebookAuthProvider.credential(withAccessToken: accessToken!)
FIRAuth.auth()?.signIn(with: credential, completion: { (user, error) in
if error != nil {
print("SIGN IN WITH FIREBASE FAILED")
completion(false)
} else {
print("YAY LOGIN SUCCESSFULL!!!!")
if let mainUser = FIRAuth.auth()?.currentUser?.providerData{
for profile in mainUser {
let providerID = profile.providerID
let uid = profile.uid // provider-specific UID
let name = profile.displayName
let email = profile.email
let photoUrl = profile.photoURL
if (FBSDKAccessToken.current() != nil) {
let facebookRequest = FBSDKGraphRequest(graphPath: "me", parameters: ["fields": "id, gender, first_name, last_name, middle_name, picture"])
facebookRequest?.start(completionHandler: { (connection, result, error) in
if error == nil {
print(result as Any)
let data = result as! NSDictionary
let gender = data.object(forKey: "gender") as! String
var newUser = User(firstName: name!, profileImageURL: ("\(photoUrl!)"), gender: gender)
newUser.save()
self.currentUserID = uid
}
})
}
completion(true)
}
}
}
})
}
}
}
That to be expected with the default login behavior. You might want to try setting loginManager.loginBehavior = .systemAccount
for a smoother login experience. That will try to login with the FB credentials you gave apple in settings, which won't require switching apps at all. If the user isn't logged into FB in settings, then it'll fall back to one of the other more intrusive methods(webivew or FB app).