groton-school / knowledgebase

Knowledgebase website
https://kb.groton.org
GNU General Public License v3.0
0 stars 0 forks source link

deal with cookie expiration #17

Closed github-actions[bot] closed 1 month ago

github-actions[bot] commented 1 year ago

https://api.github.com/groton-school/knowledgebase/blob/c4c5b14e5919f02f70b3cd448a524e775f301fcb/src/Helper/Cookies.ts#L1


// TODO deal with cookie expiration

const SEPARATOR = ';';

export const exists = (name: string) =>
  document.cookie.split(SEPARATOR).some((c) => new RegExp(`^${name}=`).test(c));

/**
 * Set cookie value
 * @param {string} name
 * @param {string} value
 * @param {(string|Date|number)} expiration Expiration date/time as text or Date object, or as a number of minutes from now
 */
export function set(
  name: string,
  value: string,
  expiration?: string | Date | number
) {
  if (!expiration) {
    expiration = new Date();
    expiration.setFullYear(expiration.getFullYear() + 1000);
  } else if (typeof expiration === 'number') {
    expiration = new Date(new Date().getTime() + expiration * 60000);
  } else {
    expiration = new Date(expiration);
  }
  document.cookie = `${name}=${value}; expires=${expiration.toUTCString()}`;
}

export function get(name: string) {
  const pattern = new RegExp(`^${name}=(.*)`);
  return document.cookie
    .split(SEPARATOR)
    .reduce((match: string | null, cookie) => {
      const matches = cookie.match(pattern);
      if (matches) {
        return matches[1];
      } else {
        return match;
      }
    }, null);
}

export const clear = (name: string) =>
(document.cookie = `${name}=; expires=${new Date(
  '1970-01-01'
).toUTCString()}`);
battis commented 1 month ago

Code re-structuring has moved this around a bit. https://github.com/groton-school/knowledgebase/blob/main/apps/ui/src/Helper/Cookies.ts