func firebaseLogin()

in authentication/LegacyAuthQuickstart/AuthenticationExampleSwift/MainViewController.swift [393:488]


  func firebaseLogin(_ credential: AuthCredential) {
    showSpinner {
      if let user = Auth.auth().currentUser {
        // [START link_credential]
        user.link(with: credential) { authResult, error in
          // [START_EXCLUDE]
          self.hideSpinner {
            if let error = error {
              self.showMessagePrompt(error.localizedDescription)
              return
            }
            self.tableView.reloadData()
          }
          // [END_EXCLUDE]
        }
        // [END link_credential]
      } else {
        // [START signin_credential]
        Auth.auth().signIn(with: credential) { authResult, error in
          // [START_EXCLUDE silent]
          self.hideSpinner {
            // [END_EXCLUDE]
            if let error = error {
              let authError = error as NSError
              if isMFAEnabled, authError.code == AuthErrorCode.secondFactorRequired.rawValue {
                // The user is a multi-factor user. Second factor challenge is required.
                let resolver = authError
                  .userInfo[AuthErrorUserInfoMultiFactorResolverKey] as! MultiFactorResolver
                var displayNameString = ""
                for tmpFactorInfo in resolver.hints {
                  displayNameString += tmpFactorInfo.displayName ?? ""
                  displayNameString += " "
                }
                self.showTextInputPrompt(
                  withMessage: "Select factor to sign in\n\(displayNameString)",
                  completionBlock: { userPressedOK, displayName in
                    var selectedHint: PhoneMultiFactorInfo?
                    for tmpFactorInfo in resolver.hints {
                      if displayName == tmpFactorInfo.displayName {
                        selectedHint = tmpFactorInfo as? PhoneMultiFactorInfo
                      }
                    }
                    PhoneAuthProvider.provider()
                      .verifyPhoneNumber(with: selectedHint!, uiDelegate: nil,
                                         multiFactorSession: resolver
                                           .session) { verificationID, error in
                        if error != nil {
                          print(
                            "Multi factor start sign in failed. Error: \(error.debugDescription)"
                          )
                        } else {
                          self.showTextInputPrompt(
                            withMessage: "Verification code for \(selectedHint?.displayName ?? "")",
                            completionBlock: { userPressedOK, verificationCode in
                              let credential: PhoneAuthCredential? = PhoneAuthProvider.provider()
                                .credential(withVerificationID: verificationID!,
                                            verificationCode: verificationCode!)
                              let assertion: MultiFactorAssertion? = PhoneMultiFactorGenerator
                                .assertion(with: credential!)
                              resolver.resolveSignIn(with: assertion!) { authResult, error in
                                if error != nil {
                                  print(
                                    "Multi factor finanlize sign in failed. Error: \(error.debugDescription)"
                                  )
                                } else {
                                  self.navigationController?.popViewController(animated: true)
                                }
                              }
                            }
                          )
                        }
                      }
                  }
                )
              } else {
                self.showMessagePrompt(error.localizedDescription)
                return
              }
              // [START_EXCLUDE]
              self.showMessagePrompt(error.localizedDescription)
              // [END_EXCLUDE]
              return
            }
            // User is signed in
            // [START_EXCLUDE]
            // Merge prevUser and currentUser accounts and data
            // ...
            // [END_EXCLUDE]
            // [START_EXCLUDE silent]
          }
          // [END_EXCLUDE]
        }
        // [END signin_credential]
      }
    }
  }