I'm having difficulty trying to use FB Login credentials to log into a Firebase Auth account when an account with the same email address already exists. Ultimately I want to link the two auth providers to the same Firebase user account but presently I can't get the FB credential to login to Firebase, if one already exists, using the code below which I think follows the Firebase documentation exactly. I have already restricted the ability for a single user (email) to have multiple accounts based on Auth Provider. When I delete the 'original' Firebase user account, Facebook login is able to login and create a user account as expected so the issues appears to only occur when there's already a Firebase auth account with the same email address from a different auth provider
Scenario
In the scenario I'm testing I've already created an email/password account (original) and am trying to add my FB account via FB Login (that uses the same email address as the email/password account) to that original account. I'm able to get the FB AccessToken and FB credential but when I pass it into Auth.auth().sign(with: credential..... it always errors out with the following error:
Error
Error Domain=FIRAuthErrorDomain Code=17012 "An account already exists with the same email address but different sign-in credentials. Sign in using a provider associated with this email address." UserInfo={FIRAuthErrorUserInfoNameKey=ERROR_ACCOUNT_EXISTS_WITH_DIFFERENT_CREDENTIAL, [email protected], FIRAuthErrorUserInfoUpdatedCredentialKey=, NSLocalizedDescription=An account already exists with the same email address but different sign-in credentials. Sign in using a provider associated with this email address.
Code
@IBAction func fbLoginButton(_ sender: FBButton) {
let loginManager = LoginManager()
loginManager.logIn(permissions: ["public_profile", "email"], from: self) { (result, error) in
if error != nil {
return
}
print(result)
guard let accessToken = AccessToken.current else {
print("Failed to get access token")
return
}
print(accessToken)
let credential = FacebookAuthProvider.credential(withAccessToken: accessToken.tokenString)
print(credential)
Auth.auth().signIn(with: credential, completion: { (firebaseUser, error) in //Can't get passed this point when another account with the same email address already exists
if error != nil {
print("Could not login into Firebase using FB credentials")
return
}
print("This is the FirebaseUser after FB Login: \(firebaseUser)")
firebaseUser?.user.link(with: credential, completion: { (authResult, error) in
if error != nil {
print("Firebase Auth Providers not linked")
return
}
// let prevUser = Auth.auth().currentUser
// Auth.auth().signIn(with: credential) { (authResult, error) in
// if let error = error {
// // ...
// return
// }
// // User is signed in
// // ...
// }
// // Merge prevUser and currentUser accounts and data
//
// ...
//This user has a profile, go to tab controller
let tabBarVC = self.storyboard?.instantiateViewController(withIdentifier: Constants.Storyboard.tabBarController)
self.view.window?.rootViewController = tabBarVC
self.view.window?.makeKeyAndVisible()
})
})
}
}