firebase-js-sdk icon indicating copy to clipboard operation
firebase-js-sdk copied to clipboard

onAuthStateChanged triggers with null user when new tab is opened

Open stanislavmboardscom opened this issue 1 year ago • 4 comments

Operating System

macos 14.4.1 (23E224)

Browser Version

Version 125.0.6422.78 (Official Build) (arm64)

Firebase SDK Version

9.23.0

Firebase SDK Product:

Auth

Describe your project's tooling

react-app-rewired

Describe the problem

onAuthStateChanged triggers with a null user when a new tab is opened

Steps and code to reproduce issue

  1. Open the page as signed-out user
  2. Sign in (I can see that onAuthStateChanged is triggered with user data)
  3. Open a new window with window.open('/anyUrl', '_blank')
  4. onAuthStateChanged is triggered with null value (application is defined user as signed out).
  5. reload the page and the user becomes authorized.

This happens only for the first iteration: f.e. if you Sign in and then reload the page and open a new tab the onAuthStateChanged is not triggered. So, to reproduce you need to open a new tab right after you sign in.

for authorization, I'm using the next code:

const persistence = data.remember
    ? browserLocalPersistence
    : browserSessionPersistence

  return setPersistence(auth, persistence).then(() =>
    signInWithEmailAndPassword(auth, data.email, data.password),
  )```

stanislavmboardscom avatar May 31 '24 09:05 stanislavmboardscom

Hi @stanislavmboardscom, thanks for reaching out to us. I tried replicating, but I wasn't able to reproduce the same behavior. Could you share a minimal app that I can run locally to replicate the issue?

jbalidiong avatar May 31 '24 15:05 jbalidiong

@jbalidiong please have a look https://github.com/stanislavmboardscom/firebase-app/tree/master

stanislavmboardscom avatar Jun 03 '24 11:06 stanislavmboardscom

@stanislavmboardscom, thank you for the minimal repro. I was able to replicate the behavior on the initial instance. I'll raise this to our Auth engineers or bring someone here that can provide more context about it. I’ll update this thread if I have any information to share.

jbalidiong avatar Jun 03 '24 14:06 jbalidiong

@jbalidiong any updates?

stanislavmboardscom avatar Jun 11 '24 10:06 stanislavmboardscom

This is a severe issue for us. It makes opening tabs difficult, and also refreshing tabs has the side effect that it logs out any other previously logged in tabs. @stanislavmboardscom did you happen to come up with a workaround?

simenandresen avatar Nov 28 '24 07:11 simenandresen

@simenandresen not really

stanislavmboardscom avatar Nov 28 '24 09:11 stanislavmboardscom

@jbalidiong any chance this could be looked at soon?

simenandresen avatar Nov 29 '24 08:11 simenandresen

I had the same problem, but if using setPersistence when persisting causes a problem, I have worked around it by using setPersistence only when I don't want to persist the authentication.

firebase: 11.0.2 macOS: 15.1 chrome: 131.0.6778.109(Official Build)

const persistence = data.remember
          ? browserLocalPersistence
          : browserSessionPersistence;
if (!data.remeber) {
    await setPersistence(auth, persistence);
}
const { user } = await signInWithEmailAndPassword(auth, email, password);

koshien2015 avatar Dec 10 '24 11:12 koshien2015

I did a little investigating and it seems like this is specifically a problem with browserLocalPersistence.

If you use browserSessionPersistence or indexedDBLocalPersistence it works as expected (original tab does not get user set to null). I don't think this is intentional but would have to double check.

I can understand that you don't want to use browserSessionPersistence because it's more limited but could you use indexedDBLocalPersistence?

What's going on with browserLocalPersistence seems weird and might indeed be a bug but if you're blocked, maybe indexedDB is an option for the moment?

hsubox76 avatar Dec 11 '24 21:12 hsubox76