Description
[REQUIRED] Describe your environment
- Operating System version: MacOS 12.6
- Browser version: Chrome 107
- Firebase SDK version: 9.14.0
- Firebase Product: auth
[REQUIRED] Describe the problem
Steps to reproduce:
I've set up a test Firebase instance and a codesandbox for easy reproduction. All I did to the Firebase instance was to create a web app, initialize auth, and add codesandbox as a trusted domain.
- Load https://codesandbox.io/s/firebase-auth-pending-redirect-bug-16oin0 and open the output in a new tab
- Open the devtools console and make sure to persist logs
- Click the button
Actual: the console log will contain window.sessionStorage\n[]
.
Expected: the console log contains window.sessionStorage\n['firebase:pendingRedirect:AIzaSyA8msq271ik2ryYJw99pZreiAWmErgBqww:[DEFAULT]']
.
Notes
When the button is clicked, that's the first time .getAuth()
is called. getAuth() apparently has an internal async task queue. One of the async tasks added to that queue is initializeCurrentUser, which tries to process a pending redirect.
But before that async task can complete, our code calls signInWithRedirect()
, which ends up creating window.sessionStorage["firebase:pendingRedirect:AIzaSyA8msq271ik2ryYJw99pZreiAWmErgBqww:[DEFAULT]"] = "true"
before initializeCurrentUser
runs.
When initializeCurrentUser
does run a few seconds later, it manages to delete the sessionStorage key before the browser redirects to the next page.
The impact of this is pretty much Firebase Auth w/Provider: Calling getRedirectResult() always returns NULL user #3115, when the user does finally get redirected back to your app, they aren't signed in because the sessionStorage key doesn't exist.
The Firebase SDK appears to assume that getAuth()
will never be called for the first time in close proximity to signInWithRedirect
. I worry some other bugs might be lurking as a result of that assumption, but this is the issue facing our app.