Facebook log in error:
"Sorry, something went wrong. We are working on it and we ll get it fixed as soon as we can."
import FirebaseAuth
import FacebookLogin
private let facebookLoginButton: FBLoginButton = {
let button = FBLoginButton()
//button.permissions = ["email","public_profile"]
button.permissions = ["public_profile", "email"]
return button
}()
override func viewDidLoad() {
super.viewDidLoad()
facebookLoginButton.delegate = self
// Add subviews
view.addSubview(scrollView)
scrollView.addSubview(imageView)
scrollView.addSubview(emailField)
scrollView.addSubview(passwordField)
scrollView.addSubview(loginButton)
scrollView.addSubview(facebookLoginButton)
}
override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
facebookLoginButton.frame = CGRect(x: 30,
y: loginButton.bottom + 10,
width: scrollView.width - 60,
height: 52)
facebookLoginButton.frame.origin.y = loginButton.bottom + 20
}
{
func loginButtonDidLogOut(_ loginButton: FBLoginButton) {
// no operation
}
func loginButton(_ loginButton: FBLoginButton, didCompleteWith result: LoginManagerLoginResult?, error: Error?) {
guard let token = result?.token?.tokenString else {
if let error = error {
print("User failed to log in with facebook: (error)")
}
return
}
let facebookRequest = FBSDKLoginKit.GraphRequest(graphPath: "me",
parameters: ["fields": "email, name"],
tokenString: token,
version: nil,
httpMethod: .get)
facebookRequest.start(completionHandler: { _, result, error in
guard let result = result as? [String: Any], error == nil else {
print("Failed to make facebook graph request")
return
}
guard let userName = result["name"] as? String,
let email = result["email"] as? String else {
print("Failed to get email and name from fb result")
return
}
let nameComponents = userName.components(separatedBy: " ")
guard nameComponents.count == 2 else {
return
}
let firstName = nameComponents[0]
let lastName = nameComponents[1]
DatabaseManager.shared.userExists(with: email, completion: { exists in
if !exists {
DatabaseManager.shared.insertUser(with: ChatAppUser(firstName: firstName,
lastName: lastName,
emailAddress: email))
}
})
let credential = FacebookAuthProvider.credential(withAccessToken: token)
FirebaseAuth.Auth.auth().signIn(with: credential, completion: { [weak self] authResult, error in
guard let self = self else {return}
guard authResult != nil, error == nil else {
if let error = error {
print("Facebook credential login failed, MFA may be needed: (error)")
}
return
}
print("Successfully logged user in")
self.navigationController?.dismiss(animated: true, completion: nil)
})
})
}
}