title: Facebook Auth "An account already exists with the same email address but different sign-in credentials."
body: <p>I have multiple login options for the user to choose from: email, google, and facebook. If Firebase already has the email stored as a user (ie. the user previously signed up with test@gmail.com), an alert that tells the user that an account with that email already exists. This works perfectly for Google sign in, as shown in this <a href="https://i.stack.imgur.com/FwYYz.png" rel="nofollow noreferrer">screenshot</a>. However, nothing visually happens when the user clicks the Facebook button (doesn't even switch screens), and I get this error in the debugger:</p>
  <pre><code>Error Domain=FIRAuthErrorDomain Code=17012 &quot;An account already exists with
  the same email address but different sign-in credentials. Sign in using a
  provider associated with this email address.&quot; UserInfo={FIRAuthErrorUserInfoNameKey=ERROR_ACCOUNT_EXISTS_WITH_DIFFERENT_CREDENTIAL,
  FIRAuthErrorUserInfoEmailKey=318junkjabr@gmail.com,
  FIRAuthErrorUserInfoUpdatedCredentialKey=&lt;FIROAuthCredential:
  0x6000005f3200&gt;, 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></pre>
  <p>This is my code for the Log In View Controller:</p>
  <pre><code>    override func viewDidLoad() {
          super.viewDidLoad()
          
          setUpFBButton()
          setUpGoogleButton()
          setUpEmailButton()
              
          GIDSignIn.sharedInstance()?.presentingViewController = self
      }
      
      
      
  // MARK: - SIGN UP WITH GOOGLE
      func sign(_ signIn: GIDSignIn!, didSignInFor user: GIDGoogleUser!, withError error: Error!) {
          
          if let err = error {
              print(&quot;Failed to log into Google: &quot;, err)
              return
          }
          print(&quot;Successfully logged into Google&quot;)
          
          guard let authentication = user.authentication else { return }
          let credential = GoogleAuthProvider.credential(withIDToken: authentication.idToken, accessToken: authentication.accessToken)
          
          // sign user in with Firebase
          Auth.auth().signIn(with: credential, completion: { (user, error) in
              let firstName = user?.user.displayName
              let email = user?.user.email
              let lastName = &quot;&quot;
              let uid = user?.user.uid
              
              if let err = error {
                  print(&quot;Failed to create a Firebase User with Google account: &quot;, err)
                  return
              } else {
                  // Successfully logged in
                  print(&quot;Successfully logged into Firebase with Google email: &quot;, email ?? &quot;&quot;, &quot;Now add user to Firestore if user is new.&quot;)
                  
                  // check if user already exists
                  self.addUserToFirestore(firstName ?? &quot;&quot;, lastName, email ?? &quot;&quot;, uid ?? &quot;&quot;, &quot;Google&quot;)
                      }
                  })
              }
      
              
          fileprivate func setUpGoogleButton() {
              Utilities.styleLightFilledButton(signInGoogleButton)
              signInGoogleButton!.addTarget(self, action:
                                  #selector(handleCustomGoogleSignIn), for: .touchUpInside)
              
              GIDSignIn.sharedInstance()?.delegate = self
              }
          
          @objc func handleCustomGoogleSignIn() {
              GIDSignIn.sharedInstance().signIn()
              }
      
      
      
      
  // MARK: - SIGN UP WITH FACEBOOK
      
      // design the facebook button and assign #selector to facebook button actions
      fileprivate func setUpFBButton() {
          Utilities.styleHollowButton(signInFacebookButton)
          
          signInFacebookButton.addTarget(self, action: #selector(handleCustomFBButton), for: .touchUpInside)
      }
      
      // handle the facebook button actions
      @objc func handleCustomFBButton() {
          LoginManager().logIn(permissions: [&quot;email&quot;, &quot;public_profile&quot;], from: self) { (result, err) in
              if err != nil {
                  print(&quot;Custom FB login failed:&quot;, err!)
                  return
              }
              self.getUserInfo()
          }
      }
      
      // grab id, name, and email of user
      func getUserInfo() {
          print(&quot;Successfully logged in with facebook...&quot;)
          GraphRequest(graphPath: &quot;/me&quot;, parameters: [&quot;fields&quot;: &quot;id, name, email&quot;]).start {
              (connection, result, err) in
              guard let Info = result as? [String: Any] else { return }
                  let name = Info[&quot;name&quot;] as? String
                  let email = Info[&quot;email&quot;] as? String
                  let uid = Info[&quot;id&quot;] as? String
              
              if err != nil {
                  print(&quot;Failed to start graph request:&quot;, err!)
                  return
              }
              print(result!)
              self.signIntoFirebase(name ?? &quot;&quot;, email ?? &quot;&quot;, uid ?? &quot;&quot;)
              
          }
      }
      
      // connect the user to firebase
      func signIntoFirebase(_ name:String, _ email:String, _ uid:String) {
             let credential = FacebookAuthProvider.credential(withAccessToken: AccessToken.current!.tokenString)
              Auth.auth().signIn(with: credential) { (user, error) in
                  if let err = error {
                      print(err)
                      return
                  } else {
              print(&quot;Facebook user successfully authenticated with Firebase. Now run through Firestore.&quot;)
              // check if user already exists. if user exists, go to chats screen. if it does not exist, create a new user and redirect to chat screen.
                      self.addUserToFirestore(name, &quot;&quot;, email, uid, &quot;Facebook&quot;)
                      
                  }
              }
          }
      
      
      func loginButtonDidLogOut(_ loginButton: FBLoginButton) {
          print(&quot;Logged out of facebook&quot;)
      }
      
      
      
  // MARK: - Other functions
      
      func addUserToFirestore(_ firstName:String, _ lastName:String, _ email:String, _ uid:String, _ signInMethod:String) {
          let db = Firestore.firestore()
          let docRef = db.collection(&quot;users&quot;).document(uid)
          // check if user exists in firestore
          docRef.getDocument { (document, error) in
              if let document = document {
                 if document.exists {
                  let message = &quot;Good news! You already have a Coal account that uses &quot; + email + &quot;.\nPlease sign in to your existing account. Then you will be able to link your &quot; + signInMethod + &quot; profile from your Account Settings page.&quot;
                  // user exists. send to chats screen.
                  print(&quot;User already exists. Document data: \(String(describing: document.data()))&quot;)
                  self.showError(&quot;You're already a member!&quot;, message)
                } else {
                  // user does not exist. create a new user
                   print(&quot;Document does not exist. Create new user.&quot;)
                  
                  docRef.setData([&quot;firstname&quot;:firstName, &quot;lastname&quot;:lastName, &quot;email&quot;:email]) { err in
                      if err != nil {
                          // Show error message
                          print(&quot;Error saving user data to Firestore&quot;)
                      } else {
                          print(&quot;New user created in Firestore&quot;)
                          self.transitionToConvo()
                      }
                  }
                }
              }
          }
      }
      
      func showError(_ title:String, _ message:String) {
          
          let alert = UIAlertController(title: title, message: message, preferredStyle: UIAlertController.Style.alert)
          alert.addAction(UIAlertAction(title: &quot;Ok&quot;, style: UIAlertAction.Style.default, handler: nil))
          self.present(alert, animated: true, completion: nil)
      }
      
      func transitionToConvo() {
          let tabBarC = self.storyboard?.instantiateViewController(withIdentifier: &quot;mainTabBarController&quot;) as! TabBarController
          tabBarC.modalPresentationStyle = .fullScreen
          self.present(tabBarC, animated: true, completion: nil)
          print(&quot;Switched to TabBarController&quot;)
      }
      
      func setUpEmailButton() {
          Utilities.styleDarkFilledButton(signInEmailButton)
      }
      
      
  } // end
  
  </code></pre>
  <p>I think the reason why it's not getting an alert is because the Facebook uid doesn't match the email uid saved in Firestore (while Google uid does match). The way I'm getting the alert to show up is if the uid matches that in Firestore, so consequently, the alert doesn't show. Does anyone know how I can get the alert to show an how to not get this error?</p>
  <p>I know my code is a bit messy, so please let me know if you need further explanation. Any help is appreciated!!</p>