Skip to content
Discussion options

You must be logged in to vote

@harvitronix

Here is my fetchWithFirebaseUser pattern.
I hope this will help.

const { data } = useSWR(user ? [`/foo/${id}`, user] : null, fetchWithUser);

export const fetchWithUser = async (url: string, user: firebase.User) => {
  const token = await user.getIdToken();

  return fetch(`${baseUrl}${url}`, {
    method: "GET",
    credentials: "include",
    headers: {
      "Content-Type": "application/json; charset=utf-8",
      Authorization: `Bearer ${token}`,
    },
  }).then((res) => {
    if (res.ok) {
      return res.json();
    } else {
      throw Error;
    }
  });
};

Replies: 3 comments 9 replies

Comment options

You must be logged in to vote
7 replies
@harvitronix
Comment options

@harvitronix
Comment options

@oiojin831
Comment options

@harvitronix
Comment options

@haarism
Comment options

Answer selected by harvitronix
Comment options

You must be logged in to vote
1 reply
@harvitronix
Comment options

Comment options

You must be logged in to vote
1 reply
@jithureddy
Comment options

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
7 participants