question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

useAuthState inconsistent persistence

See original GitHub issue

I’m not too sure what is going on but I have two components in my app that use the useAuthState hook.

const  = ({ calloutText }) => {
  const [user, loading] = useAuthState(firebase.auth());

  console.log('WelcomeOrCallout', user);

  if (user) {
    return (
      <span className="button" style={{ display: 'inline-block' }}>
        👋 Welcome back
      </span>
    );
  }

  if (loading) {
    return <Spinner />;
  }

  return <Callout text={calloutText} href={'signup'} />;
};

and

export const Links = ({ routes }) => {
  const [user, loading] = useAuthState(firebase.auth());

  console.log('Links', user);

  if (loading) {
    return null;
  }

  if (user) {
    return <UserMenu user={user} />;
  }

  return routes.map(
    ({ path, name, url, shouldHideFromNav, requiresAuth }, i) => {
      if (shouldHideFromNav) {
        return null;
      }

      if (requiresAuth && !user) {
        return null;
      }

      return (
        <>
          <li key={i}>
            <NavLink href={path} name={name} url={url} />
          </li>
          <li className="has-button">
            <Callout className="small" text={'Sign up'} />
          </li>
        </>
      );
    }
  );
};

Now, here is the weird thing, when I use useAuthState in WelcomeOrCallout, even after signing out, Links returns a user from the hook while WelcomeOrCallout does not.

If I do NOT use useAuthState in WelcomeOrCallout, Links works as expected, not rendering the UserMenu.

Is there anything I am doing wrong here that could cause this to happen? Scratching my head honestly.

Here’s my sign out page code (this is a next-js app)

export default () => {
  firebase.auth().signOut();

  return (
    <Page>
      <Content loading />
      <Redirect route="/" />
    </Page>
  );
};

Note: What’s also interesting is that if I log firebase.auth() in both components, after signing out the currentUser value is null. So it seems that the sign out is working on the firebase client end, but the hook isn’t updating correctly?

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:14 (7 by maintainers)

github_iconTop GitHub Comments

1reaction
nickrobertscommented, Apr 14, 2021

The hook fires when logging out, so it does trigger. I guess I’ll have to figure out a way to handle updating the user profile. It’s not a deal breaker, but it’s just a bit odd. 😄

0reactions
klutchdevcommented, Jul 9, 2021

are y’all still having an issue?? I have a fairly large sized ecommerce app that uses this in multiple components, along with it’s back office counterpart that uses it at the top level with context. Both apps operate properly without any notable difference and update in real time when auth is updated

Read more comments on GitHub >

github_iconTop Results From Across the Web

Authentication State Persistence | Firebase - Google
Auth state persistence specifies how a user session is persisted on a device. Whereas Firestore enablePersistence enables Cloud Firestore data caching when the ......
Read more >
How do I detect if a user is already logged in Firebase?
You can memorize last auth state to localStorage to persist it between sessions and between tabs. Then, when page starts loading, ...
Read more >
unable to resolve "firebase" from "firebase.js" - You.com | The search ...
For The First Import: error : ERROR TypeError: undefined is not an object (evaluating 'firebase.apps.length') ERROR Invariant Violation: "main" has not been ...
Read more >
Top 10 Custom React Hooks you Should Have in Your Toolbox
For a much detailed look, you can check out the useAuthState Bit component or the GitHub repo. 7. use-onClickOutside hook. An outside click...
Read more >
CHANGELOG.md · 邹昊坤/marmelab-react-admin - Gitee.com
... [Doc] Add warning about inconsistent record shapes in custom data provider ... Add row expand state persistence in <Datagrid> (4624) (fzaninotto) ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found