File size: 968 Bytes
6a7089a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
const AUTH_TOKEN_KEY = "pinchtab.auth.token";
const AUTH_REQUIRED_EVENT = "pinchtab-auth-required";

export function getStoredAuthToken(): string {
  return window.localStorage.getItem(AUTH_TOKEN_KEY)?.trim() ?? "";
}

export function setStoredAuthToken(token: string): void {
  window.localStorage.setItem(AUTH_TOKEN_KEY, token.trim());
}

export function clearStoredAuthToken(): void {
  window.localStorage.removeItem(AUTH_TOKEN_KEY);
}

export function dispatchAuthRequired(reason: string): void {
  window.dispatchEvent(
    new CustomEvent(AUTH_REQUIRED_EVENT, {
      detail: { reason },
    }),
  );
}

export function addTokenToUrl(url: string, token?: string): string {
  const authToken = (token ?? getStoredAuthToken()).trim();
  if (!authToken) {
    return url;
  }

  const absolute = new URL(url, window.location.origin);
  absolute.searchParams.set("token", authToken);
  return absolute.pathname + absolute.search;
}

export { AUTH_REQUIRED_EVENT };