onAuthStateChanged triggers with null user when new tab is opened
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
- Open the page as signed-out user
- Sign in (I can see that onAuthStateChanged is triggered with user data)
- Open a new window with
window.open('/anyUrl', '_blank') - onAuthStateChanged is triggered with
nullvalue (application is defined user as signed out). - 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),
)```
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 please have a look https://github.com/stanislavmboardscom/firebase-app/tree/master
@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 any updates?
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 not really
@jbalidiong any chance this could be looked at soon?
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);
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?