Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

What's the suggestion for token refresh? #299

Open
RobTS opened this issue Apr 19, 2023 · 1 comment
Open

What's the suggestion for token refresh? #299

RobTS opened this issue Apr 19, 2023 · 1 comment

Comments

@RobTS
Copy link

RobTS commented Apr 19, 2023

For a while, I have been wondering why my token is not refreshed automatically, despite using useIdToken. I come back to my machine after a break or the next morning, and get Auth issues.

So I did some research today and it seems (according to this docs update) that Firebase does in fact not handle it for us.

I can't seem to find a best practice suggestions in the docs of this hooks library, but believe an auto-refresh hook based on a timeout would be a great extension.

Edit: Something like that would do the trick, but uses non-public attributes:

  const [user, loading, authError] = useIdToken(firebaseWrapper.auth);
  const token = (user as { accessToken: string } | null)?.accessToken;

  useEffect(() => {
    if (!user || !token) return;
    const expiresIn = Interval.fromDateTimes(
      new Date(),
      new Date((user as any).stsTokenManager.expirationTime),
    );
    const timer = setTimeout(() => {
      user.getIdToken(true).catch(console.error);
    }, Math.max(expiresIn.length('milliseconds') - 60000, 0));
    return () => clearTimeout(timer);
  }, [user, token]);

@Jonak-Adipta-Kalita
Copy link

I am also facing with similar issues, so I am working on a Expo RN Mobile App, where I have Email auth using Firebase, it successfully, logs in and creates the user, but when I reload the app, the auth state is not logged in...

Jonak-Adipta-Kalita added a commit to Jonak-Adipta-Kalita/JAK-Mobile-App that referenced this issue Apr 30, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants