Why Gemfury? Push, build, and install  RubyGems npm packages Python packages Maven artifacts PHP packages Go Modules Debian packages RPM packages NuGet packages

Repository URL to install this package:

Details    
Size: Mime:
'use strict';

Object.defineProperty(exports, "__esModule", {
  value: true
});

var _OAuthAntiCSRFTokenHelper = require('../helpers/OAuthAntiCSRFTokenHelper');

var STORAGE_KEY = 'usersConnectorStorage:v2';

/**
 * Storage value related to web-billing.
 */
var SELECTED_FREQUENCY = 'SELECTED_FREQUENCY';

var storage = {
  /**
   * Reads and parses the user state from the local storage.
   * Returns null if the local storage does not contain any state.
   * @returns {object}
   */
  loadState: function loadState() {
    try {
      var rawStorage = localStorage.getItem(STORAGE_KEY);
      return JSON.parse(rawStorage);
    } catch (e) {
      return null;
    }
  },


  /**
   * Stores the partial user state in the local storage.
   * @param {object} state The user state partial.
   */

  storeState: function storeState(state) {
    localStorage.setItem(STORAGE_KEY, JSON.stringify(state));
  },


  /**
   * Cleans up the user state from the local storage.
   */
  deleteState: function deleteState() {
    localStorage.removeItem(STORAGE_KEY);
    localStorage.removeItem(SELECTED_FREQUENCY);
  },


  /**
   * Forces a cookie to expire by settings its expiration to now.
   * @param {string} key The name of the cookie to remove
   * @param {string?} options.domain The optional cookie domain
   */
  deleteCookie: function deleteCookie(key) {
    var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};

    var path = options.path || '/';
    var domain = options.domain || '.' + document.location.hostname // always start with '.'
    .split('.').splice(-2) // remove everything except the TLD ('com') and the second level domain ('doodle')
    .join('.');
    var expires = new Date(0).toGMTString(); // delete the cookie

    document.cookie = key + '=;path=' + path + ';domain=' + domain + ';expires=' + expires;
  },


  /**
   * Reads a cookie and returns its value. Defaults to undefined.
   * @param {string} key The key of the cookie to retrieve
   * @param {() -> string} [options.getCookie] An optional function used to read the cookie. `document.cookie` will be used otherwise.
   * @returns {string|undefined}
   */
  getCookie: function getCookie(key) {
    var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};

    var cookies = (options.getCookie && options.getCookie() || document.cookie).split('; ');

    return cookies.reduce(function (result, cookie) {
      var searchKey = key + '=';

      if (cookie.indexOf(searchKey) === 0) {
        return cookie.substring(searchKey.length);
      }

      return result;
    }, undefined);
  },


  /**
   * Sets a cookie with key `oauth_anti_csrf_token_cookie` and the given token as value.
   * @param {string} token The token value
   * @param {string} domain The domain in which to set the cookie
   */
  setOAuthAntiCSRFTokenCookie: function setOAuthAntiCSRFTokenCookie(token, domain) {
    var expires = (0, _OAuthAntiCSRFTokenHelper.generateOAuthAntiCSRFCookieDuration)();
    var cookie = 'oauth_anti_csrf_token_cookie=' + token + ';expires=' + expires + ';path=/';
    if (domain) {
      cookie += ';domain=' + encodeURIComponent(domain);
    }
    document.cookie = cookie;
  },


  /**
   * Sets a cookie with key `token` and the given token as value.
   * @param {string} token The token value
   * @param {string} domain The domain in which to set the cookie
   */
  setAntiCSRFTokenCookie: function setAntiCSRFTokenCookie(token, domain) {
    // we delete the domain-less cookie, just in case
    document.cookie = 'token=DumpMe;max-age=-1';

    var cookie = 'token=' + token + ';path=/;Secure;SameSite=Lax';
    if (domain) {
      cookie += ';domain=' + encodeURIComponent(domain);
    }
    document.cookie = cookie;
  }
};

exports.default = storage;