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.

onAuthStateChange does not catch auth state changes across multiple windows

See original GitHub issue

Bug report

Describe the bug

supabaseClient.auth.onAuthStateChange() does not work across multiple windows.

To Reproduce

Steps to reproduce the behavior, please provide code snippets or a repository:

  1. Create an auth.onAuthStateChange() listener with Supabase in a react app.
const { data: authListener } = supabaseClient.auth.onAuthStateChange(
      async (event, session) => {
        console.log(`UserContext.onAuthStateChange event=${event}`);
      }
);
  1. Open two separate browser windows (where you haven’t signed in yet in both windows)
  2. Sign in from one of the two windows
  3. The user will be signed in from that window. SIGNED_IN event will be fired in the window where you sign in, but the other browser window does not catch the SIGNED_IN event.

Expected behavior

If the auth state changes in any of the windows, the change should be broadcasted to all windows. Firebase Auth broadcasts auth state changes to all windows. The current onAuthStateChange() listener requires a developer to poll auth state if a user has multiple windows open.

Screenshots

If applicable, add screenshots to help explain your problem.

System information

  • OS: Windows
  • Browser (if applies) Chrome
  • Version of supabase-js: 1.21.0

Additional context

The current onAuthStateChange() listener requires a developer to poll auth state if a user has multiple windows open. An example of this being an issue is if the user clicks on the magic link. This will sign in the user in a new window and leave the old window with a stale screen.

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Reactions:1
  • Comments:8 (4 by maintainers)

github_iconTop GitHub Comments

5reactions
i-pipcommented, Aug 25, 2021

Hi @subwaymatch, you can fix this using a window.onstorage event listener and listening for an event with key supabase.auth.token.

Here’s a code snippet:

const [session, setSession] = useState(null);

  useEffect(() => {
    setSession(supabase.auth.session());
    supabase.auth.onAuthStateChange((_event, session) => {
      setSession(session);
    });
    window.onstorage = (e) => {
      if (e.key === "supabase.auth.token") {
        const newSession = JSON.parse(e.newValue);
        setSession(newSession?.currentSession);
      }
    };
  }, []); 

This will work for all auth change events

@kiwicopple, I’m working on adding this into gotrue-js so that supabase.auth.onAuthStateChange takes care of this automagically

1reaction
kiwicopplecommented, Dec 28, 2021

this should now be working on the latest version of supabase-js 🎉

https://github.com/supabase/gotrue-js/pull/184

Read more comments on GitHub >

github_iconTop Results From Across the Web

javascript - onAuthStateChanged inconsistent
An auth state change event is triggered and my event handler function, that I registered ... I'm not sure what would be happening...
Read more >
Authentication State Persistence | Firebase - Google
As stated above, there are multiple situations where the default permanent persistence may need to be overridden. Note: Do not confuse Auth state...
Read more >
29 - Using onAuthStateChanged to Detect User at App Startup
In this video, we improve our log in system so that it detects if a user's info is saved in local storage by...
Read more >
Using Firebase Authentication
There are three methods for listening to authentication state changes: ... or user-not-found exception that you can catch and handle in your app...
Read more >
Authentication
The Firebase JS SDK is now in BETA! This EAP site is no longer maintained. See the official Firebase Documentation site for the...
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