All files / src/lib/auth token.ts

56.25% Statements 18/32
50% Branches 1/2
50% Functions 1/2
56.25% Lines 18/32

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 331x 1x 1x 1x                             1x 1x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x  
const ACCESS_TOKEN_KEY = 'accessToken';
const COOKIE_DOMAIN = '.wego.monster';
 
export const setAccessToken = (token: string, maxAgeSeconds?: number) => {
  if (typeof document === 'undefined') return;

  const parts = [
    `${ACCESS_TOKEN_KEY}=${encodeURIComponent(token)}`,
    'path=/',
    `domain=${COOKIE_DOMAIN}`,
  ];

  if (typeof maxAgeSeconds === 'number' && maxAgeSeconds > 0) {
    parts.push(`Max-Age=${maxAgeSeconds}`);
  }

  document.cookie = parts.join('; ');
};
 
export const clearAccessToken = () => {
  if (typeof document === 'undefined') return;
 
  document.cookie = [
    `${ACCESS_TOKEN_KEY}=`,
    'Max-Age=0',
    'Expires=Thu, 01 Jan 1970 00:00:00 GMT',
    'path=/',
    `domain=${COOKIE_DOMAIN}`,
  ].join('; ');
 
  document.cookie = `${ACCESS_TOKEN_KEY}=; Max-Age=0; path=/`;
};