All files / src/utils cookies.ts

100% Statements 24/24
100% Branches 6/6
100% Functions 3/3
100% Lines 24/24

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 881x     1x     1x                                                                                       1x 1x 1x   1x 1x                 1x 1x 1x 1x 1x 1x 1x               1x 3x 3x 2x 2x 3x 1x 1x 3x  
import Cookies from 'js-cookie';
 
// Constants
import { COOKIE_KEYS } from '@/constants';
 
// Utils
import { cookieStorage } from '@shared/utils';
 
export interface CookieAttributes {
  /**
   * Define when the cookie will be removed. Value can be a Number
   * which will be interpreted as days from time of creation or a
   * Date instance. If omitted, the cookie becomes a session cookie.
   */
  expires?: number | Date | undefined;
 
  /**
   * Define the path where the cookie is available. Defaults to '/'
   */
  path?: string | undefined;
 
  /**
   * Define the domain where the cookie is available. Defaults to
   * the domain of the page where the cookie was created.
   */
  domain?: string | undefined;
 
  /**
   * A Boolean indicating if the cookie transmission requires a
   * secure protocol (https). Defaults to false.
   */
  secure?: boolean | undefined;
 
  /**
   * Asserts that a cookie must not be sent with cross-origin requests,
   * providing some protection against cross-site request forgery
   * attacks (CSRF)
   */
  sameSite?: 'strict' | 'Strict' | 'lax' | 'Lax' | 'none' | 'None' | undefined;
 
  [property: string]: unknown;
}
 
/**
 * Get the latest shared authentication cookies.
 *
 * @returns {Object} An object containing:
 *  - tokenShared {string | null} The shared auth token stored in cookies.
 *  - emailShared {string | null} The shared user email stored in cookies.
 */
export const getSharedCookies = () => {
  const tokenShared = cookieStorage.getItem(COOKIE_KEYS.TOKEN_SHARED);
  const emailShared = cookieStorage.getItem(COOKIE_KEYS.EMAIL_USER_SHARED);
 
  return { tokenShared, emailShared };
};
 
/**
 * Save an array of strings into a cookie as a JSON string.
 *
 * @param key - The cookie key (name) under which the array will be stored.
 * @param value - The array of strings to store in the cookie.
 * @param options - Optional cookie attributes (e.g., expires, path, secure).
 */
export const setCookieArray = (
  key: string,
  value: string[],
  options?: CookieAttributes,
) => {
  Cookies.set(key, JSON.stringify(value), options);
};
 
/**
 * Retrieve an array of strings from a cookie.
 *
 * @param key - The cookie key (name) to read.
 * @returns An array of strings parsed from the cookie, or an empty array if not found or invalid.
 */
export const getCookieArray = (key: string): string[] => {
  const cookieValue = Cookies.get(key);
  if (!cookieValue) return [];
  try {
    return JSON.parse(cookieValue);
  } catch {
    return [];
  }
};