firebaseui.auth.widget.handler.handlePhoneSignInStart = function()

in javascript/widgets/handler/phonesigninstart.js [47:184]


firebaseui.auth.widget.handler.handlePhoneSignInStart = function(
    app, container, opt_phoneNumberValue, opt_infoBarMessage) {
  // Get the developer's reCAPTCHA configuration.
  var recaptchaParameters = app.getConfig().getRecaptchaParameters() || {};
  /** @private {?string} The reCAPTCHA response token. */
  firebaseui.auth.widget.handler.recaptchaToken_ = null;
  /**
   * @private {boolean} Whether visible reCAPTCHA is enabled. Default is true.
   */
  firebaseui.auth.widget.handler.enableVisibleRecaptcha_ =
      !(recaptchaParameters && recaptchaParameters['size'] === 'invisible');

  var isPhoneProviderOnly =
      firebaseui.auth.widget.handler.common.isPhoneProviderOnly(app);
  // Set the default values for the phone number input, getting it first from
  // the passed in parameters and second from the config.
  var defaultCountry = app.getConfig().getPhoneAuthDefaultCountry();
  // Get default national number only if phone auth is the only provider.
  // This will be overridden by opt_phoneNumberValue if available as it has
  // higher priority.
  var defaultNationalNumber = isPhoneProviderOnly ?
      app.getConfig().getPhoneAuthDefaultNationalNumber() : null;
  var countryId = (opt_phoneNumberValue && opt_phoneNumberValue.countryId) ||
      (defaultCountry && defaultCountry.e164_key) || null;
  var nationalNumber = (opt_phoneNumberValue &&
      opt_phoneNumberValue.nationalNumber) || defaultNationalNumber;
  var availableCountries = app.getConfig().getPhoneAuthAvailableCountries();
  if (availableCountries) {
     firebaseui.auth.data.country.sortCountryListForLocale(
         availableCountries, goog.LOCALE);
  }
  /**
   * @private {!firebaseui.auth.data.country.LookupTree} The country
   *     lookup prefix tree to search country code with.
   */
  firebaseui.auth.widget.handler.lookupTree_ = availableCountries ?
      new firebaseui.auth.data.country.LookupTree(
          /** @type {!Array<!firebaseui.auth.data.country.Country>} */
          (app.getConfig().getPhoneAuthAvailableCountries())) :
      firebaseui.auth.data.country.LOOKUP_TREE;
  // Render the phone sign in start page component.
  var component = new firebaseui.auth.ui.page.PhoneSignInStart(
      // On submit.
      function(e) {
        firebaseui.auth.widget.handler.onPhoneSignInStartSubmit_(
            app,
            component,
            recaptchaVerifier,
            // Whether the submission was triggered by a key code.
            !!(e && e.keyCode));
      },
      firebaseui.auth.widget.handler.enableVisibleRecaptcha_,
      // On cancel.
      isPhoneProviderOnly ? null : function(e) {
        // Go back to start sign in handler.
        recaptchaVerifier.clear();
        component.dispose();
        firebaseui.auth.widget.handler.common.handleSignInStart(
            app, container);
      },
      app.getConfig().getTosUrl(),
      app.getConfig().getPrivacyPolicyUrl(),
      isPhoneProviderOnly,
      firebaseui.auth.widget.handler.lookupTree_,
      countryId,
      nationalNumber);
  component.render(container);
  // Set current UI component.
  app.setCurrentComponent(component);
  // Show info bar if necessary.
  if (opt_infoBarMessage) {
    component.showInfoBar(opt_infoBarMessage);
  }

  // Add callbacks to reCAPTCHA parameters to listen to token changes.
  recaptchaParameters['callback'] = function(recaptchaToken) {
    // Hide any reCAPTCHA error if available.
    if (component.getRecaptchaErrorElement()) {
      firebaseui.auth.ui.element.hide(
          component.getRecaptchaErrorElement());
    }
    // Update the reCAPTCHA response token.
    firebaseui.auth.widget.handler.recaptchaToken_ = recaptchaToken;
    if (!firebaseui.auth.widget.handler.enableVisibleRecaptcha_) {
      // If invisible and token ready, submit request.
      // This is needed since when the button is clicked and no response is
      // available, the response may take time to be provisioned either without
      // any user action or by waiting for the challenge to be resolved.
      firebaseui.auth.widget.handler.onPhoneSignInStartSubmit_(
          app,
          component,
          recaptchaVerifier);
    }
  };
  recaptchaParameters['expired-callback'] = function() {
    // On expiration, reset reCAPTCHA token.
    firebaseui.auth.widget.handler.recaptchaToken_ = null;
  };
  // Initialize a reCAPTCHA verifier instance.
  var recaptchaVerifier = new firebase.auth['RecaptchaVerifier'](
      // reCAPTCHA container: either the visible reCAPTCHA element or the submit
      // button for the invisible one.
      firebaseui.auth.widget.handler.enableVisibleRecaptcha_ ?
          component.getRecaptchaElement() :
          component.getSubmitElement(),
      // reCAPTCHA parameters.
      recaptchaParameters,
      // Use external auth instance. This is OK since on completion, no linking
      // could be required or no additional profile update is needed and it is
      // safe to interrupt.
      app.getExternalAuth().app);
  // Render reCAPTCHA verifier.
  app.registerPending(component.executePromiseRequest(
      /** @type {function (): !goog.Promise} */ (
          goog.bind(recaptchaVerifier.render, recaptchaVerifier)
      ),
      [],
      function(widgetId) {
        // reCAPTCHA rendered successfully. Save the corresponding widget ID.
        /** @private {number} The reCAPTCHA widget ID. */
        firebaseui.auth.widget.handler.recaptchaWidgetId_ =
            /** @type {number} */ (widgetId);
      },
      function(error) {
        if (error['name'] && error['name'] == 'cancel') {
          return;
        }
        // We need to force re-rendering of the reCAPTCHA, this is why we have
        // to go back to the previous page.
        // This could happen in a bad network situation. The user will try again
        // on network availability and the reCAPTCHA would render this time.
        var errorMessage =
            firebaseui.auth.widget.handler.common.getErrorMessage(error);
        component.dispose();
        firebaseui.auth.widget.handler.common.handleSignInStart(
            app, container, undefined, errorMessage);
      }));
};