firebase / firebase/firebase-js-sdk

onAuthStateChanged triggers with null user when new tab is opened

Open
#8,282 9 comments 5 reactions 0 assignees View on GitHub
api: auth bug stack:React
Dominant language
TypeScript
Stars
5.1k
Forks
1k
Avg merge
2d 21h
Merged PRs (30d)
37

Description

### 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),
)```

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.