Firebase: Can I just use getAuth() in every file that I need the auth object in?

Viewed 37

I'm using Firebase Authentication. Should I have one auth = getAuth() in the root App component and pass that around via Context, or can I just call auth = getAuth() in every file that needs it? I'm using the second approach right now because it's simpler, and it seems to work; the idToken that's returned is the same across different files/pages.

More questions:

  1. Does getAuth() have to be in a component, or can it be outside? Does it have to be in a useEffect so it’s only called once?
  2. Why is auth.currentUser.accessToken marked by TypeScript as not existing? But when I console.log currentUser, the accessToken key is there. I can ignore TypeScript’s error and just use currentUser.accessToken and it works. It’s more convenient than using .getIdToken(), which I have to await.
0 Answers
Related