async function authenticatedFetch()

in frontend/app/auth.js [7:27]


async function authenticatedFetch(input, init) {
  const token = localStorage.getItem("fibrecensus:access-token");
  if (!token) {
    console.log("No local access token, performing request without it");
    return fetch(input, init);
  }

  const toAddTo = init ?? {};

  const existingHeaders = toAddTo.hasOwnProperty("headers")
    ? toAddTo.headers
    : {};

  const newInit = Object.assign({}, toAddTo, {
    headers: Object.assign({}, existingHeaders, {
      Authorization: `Bearer ${token}`,
    }),
  });

  return fetch(input, newInit);
}