Manual Reference Source Test

src/modules/identity-redirect.js

/**
 * Handle navigating the user
 */
class IdentityRedirect {
  /**
   * @param {Object} props
   * @param {Object} serverController
   * @param {Object} popUpController
   * @param {Object} iframeController
   */
  constructor(props, serverController, popUpController, iframeController) {
    this._globalProps = props;
    this._serverController = serverController;
    this._popUpController = popUpController;
    this._iframeController = iframeController;
    this._redirectMap = {
      debug: 'debug',
      env: 'env',
      fbToken: 'facebookToken',
      Identity: 'version',
      key: 'key',
      marketingReferrer: 'marketingReferrer',
      nbcIdentityCrossApp: 'crossApp',
      nbcIdentityEdit: 'propertyEdit',
      nbcIdentityLogout: 'logout',
      nbcIdentityNative: 'native',
      nbcIdentityPopUp: 'enablePopUp',
      nbcIdentityIframe: 'enableIframe',
      nbcIdentityToken: 'token',
      nbcIdentityDefaultPage: 'defaultPage',
      origin: 'origin',
      redirect: 'redirectUrl',
      properties: 'properties',
      topbar: 'topbar',
      useLocalConfig: 'useLocalConfig',
      requestPrompt: 'requestPrompt',
      configLocationHost: 'configLocationHost',
    };
  }

  /**
   * Update global prop after init
   *
   * @param {String} name
   * @param {Any} value
   */
  updateGlobalProp(name, value) {
    this._globalProps[name] = value;
  }

  /**
   * Redirect user to authenticate app
   *
   * @param {Object} params
   */
  trigger(params) {
    const combinedParams = Object.assign({}, this._globalProps, params);
    if (!combinedParams.version) return;
    const {enablePopUp, enableIframe} = combinedParams;
    const urlParams = new URLSearchParams();

    Object.keys(this._redirectMap).forEach((key) => {
      const paramKey = this._redirectMap[key];
      if (combinedParams[paramKey]) {
        let value;

        switch (key) {
          case 'properties':
            value = combinedParams[paramKey].join(',');
            break;
          case 'token':
            if (!this._useCookie) value = combinedParams[paramKey];
            break;
          default:
            value = combinedParams[paramKey];
        }

        if (value) urlParams.set(key, value);
      } else {
        let value;

        switch (key) {
          case 'topbar':
            if (combinedParams[paramKey] === false) value = combinedParams[paramKey];
            break;
        }

        if (typeof value !== 'undefined') urlParams.set(key, value);
      }
    });

    // Set url to local path or production path
    const urlString = /^(3|4)[0-9]{1,3}$/.test(location.port) ?
      `http://${location.hostname}:4000` : `v${combinedParams.version}/authenticate/`;
    const url = new URL(urlString, this._serverController.getSdkUrl());
    url.hash = urlParams.toString();
    const href = url.toString();

    if (enablePopUp) {
      this._popUpController.setTarget(url.origin);
      this._popUpController.open(href, 'NBCUniversal Authenticate', 375, 750);
    } else if (enableIframe) {
      this._iframeController.setTarget(url.origin);
      this._iframeController.openIframe(href, combinedParams.propertyEdit);
    } else {
      location.href = href;
    }
  }

  /**
   * Parse any Identity url parameters and save the results
   *
   * @return {string} token
   */
  parseUrlParameters() {
    let token;
    let result;
    let optIns = [];
    const urlParams = new URLSearchParams(location.hash.substr(1));
    const urlParamNames = [
      'nbcIdentityResult',
      'nbcIdentityHash',
    ];
    if (!this._useCookie) {
      urlParamNames.push('nbcIdentityToken');
      urlParamNames.push('nbcIdentityOptIns');
    }

    // Loop through the URL param names and check if any exist
    let urlParamExists = false;
    urlParamNames.some((name) => {
      if (urlParams.has(name)) urlParamExists = true;
      return urlParamExists;
    });

    // Only continue if at least one of the params exist
    if (urlParamExists) {
      result = urlParams.get(urlParamNames[0]);
      const hash = urlParams.get(urlParamNames[1]);
      if (!this._useCookie) {
        token = urlParams.get(urlParamNames[2]);
        if (urlParams.get(urlParamNames[3])) optIns = urlParams.get(urlParamNames[3]).split(',');
      }

      // Remove params from hash
      urlParamNames.forEach((name) => urlParams.delete(name));
      location.hash = `${hash||''}${hash && urlParams.toString() !== '' ? '&' : ''}${urlParams||''}`;
    }

    return {
      optIns,
      token,
      result,
    };
  }
}

export default IdentityRedirect;