selectTenant()

in javascript/widgets/firebaseuihandler.js [228:333]


  selectTenant(projectConfig, tenantIds) {
    this.disposeCurrentComponent_();
    const apiKey = projectConfig['apiKey'];
    return new GoogPromise((resolve, reject) => {
      if (!this.configs_.hasOwnProperty(apiKey)) {
        const error =
            new Error('Invalid project configuration: API key is invalid!');
        // Add error code for localization.
        error['code'] = 'invalid-configuration';
        this.handleError(error);
        reject(error);
        return;
      }
      const selectTenantUiHidden =
          this.configs_[apiKey].getSelectTenantUiHiddenCallback();
      // Option first flow.
      if (this.configs_[apiKey].getDisplayMode() ===
          UiHandlerConfig.DisplayMode.OPTION_FIRST) {
        // Get the button configurations based on the given tenant IDs.
        const tenantButtonConfigs = [];
        tenantIds.forEach((tenantId) => {
          const buttonConfig =
              this.configs_[apiKey].getSelectionButtonConfigForTenant(
                  tenantId || UiHandlerConfig.ConfigKeys.TOP_LEVEL_CONFIG_KEY);
          if (buttonConfig) {
            tenantButtonConfigs.push(buttonConfig);
          }
        });
        // Resolver to return the SelectedTenantInfo based on the tenantId.
        const resolveWithTenantInfo = (tenantId) => {
          const selectedTenantInfo = {
            'tenantId': tenantId,
            'providerIds':
                this.configs_[apiKey].getProvidersForTenant(
                    tenantId ||
                    UiHandlerConfig.ConfigKeys.TOP_LEVEL_CONFIG_KEY),
          };
          resolve(selectedTenantInfo);
        };
        // If only one tenant is available, do not show the select tenant page.
        // Resolve with the only tenant and return immediately.
        if (tenantButtonConfigs.length === 1) {
          const tenantId = tenantButtonConfigs[0].tenantId;
          resolveWithTenantInfo(tenantId);
          return;
        } else {
          const onTenantClick = (tenantId) => {
            this.disposeCurrentComponent_();
            // Trigger the selectTenantUiHidden callback.
            if (selectTenantUiHidden) {
              selectTenantUiHidden();
            }
            resolveWithTenantInfo(tenantId);
          };
          this.currentComponent_ =
              new SelectTenant(onTenantClick, tenantButtonConfigs,
                               this.configs_[apiKey].getTosUrl(),
                               this.configs_[apiKey].getPrivacyPolicyUrl());
        }
      } else {
        // Identifier first flow.
        const onEmailEnter = () => {
          const email = this.currentComponent_.checkAndGetEmail();
          if (!email) {
            return;
          }
          for (let i = 0; i < tenantIds.length; i++) {
            const providers =
                this.configs_[apiKey].getProvidersForTenant(
                    tenantIds[i] ||
                    UiHandlerConfig.ConfigKeys.TOP_LEVEL_CONFIG_KEY,
                    email);
            // Resolve with the first matching tenant with available providers.
            if (providers.length !== 0) {
              const selectedTenantInfo = {
                'tenantId': tenantIds[i],
                'providerIds': providers,
                'email': email,
              };
              this.disposeCurrentComponent_();
              // Trigger the selectTenantUiHidden callback.
              if (selectTenantUiHidden) {
                selectTenantUiHidden();
              }
              resolve(selectedTenantInfo);
              return;
            }
          }
          // If no matching tenant found, show error message in info bar.
          this.currentComponent_.showInfoBar(
              getLocalizedErrorMessage('no-matching-tenant-for-email'));
        };
        this.currentComponent_ = new ProviderMatchByEmail(
            onEmailEnter,
            this.configs_[apiKey].getTosUrl(),
            this.configs_[apiKey].getPrivacyPolicyUrl());
      }
      this.currentComponent_.render(this.container_);
      // Trigger the selectTenantUiShown callback.
      const selectTenantUiShown =
          this.configs_[apiKey].getSelectTenantUiShownCallback();
      if (selectTenantUiShown) {
        selectTenantUiShown();
      }
    });
  }