utils/cookie.js

  1. /*
  2. * Your installation or use of this SugarCRM file is subject to the applicable
  3. * terms available at
  4. * http://support.sugarcrm.com/Resources/Master_Subscription_Agreements/.
  5. * If you do not agree to all of the applicable terms or do not have the
  6. * authority to bind the entity as an authorized representative, then do not
  7. * install or use this SugarCRM file.
  8. *
  9. * Copyright (C) SugarCRM Inc. All rights reserved.
  10. */
  11. const Utils = require('utils/utils');
  12. let path = window.location.pathname.replace(/\/[^\/]*$/, '/cookie.html');
  13. let getCookie = function() {
  14. return {};
  15. };
  16. /**
  17. * Cookie storage for authentication data.
  18. *
  19. * @module Utils/Cookie
  20. */
  21. /**
  22. * @alias module:Utils/Cookie
  23. */
  24. const Cookie = {
  25. /**
  26. * Initializes cookie support and executes code after it is ready.
  27. *
  28. * @param {Function} callback Called after cookie support is ready.
  29. */
  30. initAsync: function(callback) {
  31. var frame = document.createElement('iframe');
  32. frame.style.display = 'none';
  33. frame.src = path;
  34. document.body.appendChild(frame);
  35. frame.contentWindow.onload = function() {
  36. getCookie = function() {
  37. return frame.contentWindow.getCookie();
  38. };
  39. callback();
  40. };
  41. },
  42. /**
  43. * Checks if the item exists in storage.
  44. *
  45. * @param {string} key Item key.
  46. * @return {boolean} Whether the item exists in storage.
  47. */
  48. has: function(key) {
  49. return key in getCookie();
  50. },
  51. /**
  52. * Gets an item from storage.
  53. *
  54. * @param {string} key Item key.
  55. * @return {string} Item with the given key.
  56. */
  57. get: function(key) {
  58. return getCookie()[key];
  59. },
  60. /**
  61. * Puts an item into storage.
  62. *
  63. * @param {string} key Item key.
  64. * @param {string} value Item to put.
  65. */
  66. set: function(key, value) {
  67. Utils.cookie.setCookie(key, value, null, path);
  68. },
  69. /**
  70. * Deletes an item from storage.
  71. *
  72. * @param {string} key Item key.
  73. */
  74. cut: function(key) {
  75. Utils.cookie.setCookie(key, '', -1, path);
  76. }
  77. };
  78. module.exports = Cookie;
  79. JAVASCRIPT
    Copied!