1 import isTruthy from '@proton/utils/isTruthy';
3 export const getCookies = (): string[] => {
5 return document.cookie.split(';').map((item) => item.trim());
11 export const getCookie = (name: string, cookies = document.cookie) => {
12 return `; ${cookies}`.match(`;\\s*${name}=([^;]+)`)?.[1];
15 export enum CookieSameSiteAttribute {
21 export interface SetCookieArguments {
23 cookieValue: string | undefined;
24 cookieDomain?: string;
25 expirationDate?: string;
28 samesite?: CookieSameSiteAttribute;
31 export const setCookie = ({
33 cookieValue: maybeCookieValue,
34 expirationDate: maybeExpirationDate,
37 samesite = CookieSameSiteAttribute.Lax,
39 }: SetCookieArguments) => {
40 const cookieValue = maybeCookieValue === undefined ? '' : maybeCookieValue;
42 let expirationDate = maybeExpirationDate;
44 if (expirationDate === 'max') {
45 /* https://en.wikipedia.org/wiki/Year_2038_problem */
46 expirationDate = new Date(2147483647000).toUTCString();
49 expirationDate = maybeCookieValue === undefined ? new Date(0).toUTCString() : expirationDate;
52 `${cookieName}=${cookieValue}`,
53 expirationDate && `expires=${expirationDate}`,
54 cookieDomain && `domain=${cookieDomain}`,
55 path && `path=${path}`,
57 samesite && `samesite=${samesite}`,
63 export const deleteCookie = (cookieName: string) => {
66 cookieValue: undefined,