just to start off with I have looked at many examples but can't seem to find a solution. Well I thought I did but it doesn't seem to work.
What I'm trying to do is use the Firebase auth login method for facebook, which works by the way. My issue is I want to link the password auth credentials and facebook auth credentials when the facebook method throws the error that the email/credentials already exist. I read here that I could use error.email
but it doesn't give me the option to access the email.error
. I might be missing something and I have spent a lot of time trying to figure it out.
Here is my code:
func signInFacebookUser() {
let fbLoginManager = FBSDKLoginManager()
fbLoginManager.logIn(withReadPermissions: ["public_profile", "email"], from: self) { (result, error) in
if let error = error {
self.errorMessagePopUp(title: "Failed to login", message: error)
return
}
guard let accessToken = FBSDKAccessToken.current() else {
print("Failed to get access token")
return
}
let credential = FacebookAuthProvider.credential(withAccessToken: accessToken.tokenString)
Auth.auth().signIn(with: credential, completion: { (user, error) in
if let error = error {
let errCode = AuthErrorCode(rawValue: error._code)!
switch errCode {
case .accountExistsWithDifferentCredential:
// somehow obtain email right here.
self.showSigninForm(attributes: self.attributes(), style: .light)
return
default:
return
}
}
self.performSegue(withIdentifier: "signInBtnPressed", sender: nil)
})
}
}
Try this code
@IBAction func facebookLoginButtonTapped(_ sender: UIButton) {
if Utility.checkInternet(){
let fbLoginManager: FBSDKLoginManager = FBSDKLoginManager()
fbLoginManager.logIn(withReadPermissions: ["email", "public_profile"], from: self) { (result, error) in
if (error == nil){
let fbLoginResult: FBSDKLoginManagerLoginResult = result!
if fbLoginResult.grantedPermissions != nil {
if(fbLoginResult.grantedPermissions.contains("email")){
self.facebookLogin()
}
}
}
}
}
else{
Utility.showAlert("Connection Error!", message: "Please check internet connection and retry.", viewController: (self.window?.rootViewController)!)
}
}
func facebookLogin() {
Utility.showProgress("")
if((FBSDKAccessToken.current()) != nil) {
FBSDKGraphRequest(graphPath: "me", parameters: ["fields":"id, name, first_name, last_name, picture.type(large), email "]).start(completionHandler: { (connection, result, error) in
if result == nil {
}
else{
self.userResults = result as! [String : AnyObject]
self.email = (self.userResults["email"] as? String) ?? ""
if (self.email != "") {
}
else{
Utility.showAlert("Login Faild", message: error?.localizedDescription, viewController: self)
}
}
})
}
}
This Works for me Swift4
@objc func loginButtonClicked() {
var alertController = UIAlertController()
let loginManager = LoginManager()
loginManager.logIn(readPermissions: [ReadPermission.email], >viewController: self, completion: {loginResult in
switch loginResult {
case .failed(let error):
print(error)
case .cancelled:
print("User cancelled login.")
case .success(_, _, let accessToken):
print(accessToken)
}
})
}
func getDataUser() {
let request = GraphRequest(graphPath: "me?fields=first_name,last_name,email",
parameters:[:],
httpMethod: .GET)
request.start { httpResponse, result in
switch result {
case .success(let response): do {
print("Graph Request Succeeded: \(response)")
let dicval = response.dictionaryValue
let email = dicval?.keys
print("Graph Request Succeeded: \(String(describing: email))")
}
case .failed(let error):
print("Graph Request Failed: \(error)")
}
}
}
}