Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Firebase 9.6 in web worker: auth.onAuthStateChanged called once, user always null #5791

Closed
rejhgadellaa opened this issue Dec 5, 2021 · 3 comments · Fixed by #5811
Closed

Comments

@rejhgadellaa
Copy link

rejhgadellaa commented Dec 5, 2021

[REQUIRED] Describe your environment

  • Operating System version: Windows 10
  • Browser version: Chrome 96.0.4664.45
  • Firebase SDK version: 9.6.0 (compat)
  • Firebase Product: auth

[REQUIRED] Describe the problem

Steps to reproduce:

I have a web app where auth is handled in a web worker. I use Comlink to call methods in the worker from the main thread.

I use firebase-auth-ui 6.0.0 for the (you guessed it) auth ui.

Find the relevant example code below. (note: I've extracted and simplified the code so there may be a typo/etc in there)

Before I switched to Firebase 9 (compat), this worked fine.

After uprading from 8.10 to 9.6, the onAuthStateChanged callback is called once with the user object = null. Since I was already logged in before (prior to upgrading to 9.6), I expect the callback to be called with a User object.

At this point my app loads the auth-ui and displays it. When I try to (re)log in using auth-ui, the callback is never called with the user object. From what I can tell, the auth-ui library does successfully log me in, but nothing happens in my web app.

Reloading the web app doesn't help either. I also tried clearing the application data (Dev tools > Application > Storage > Clear site data (with every option checked)).

Relevant Code:

/* Web worker */

import * as Comlink from 'comlink';
import firebase from 'firebase/compat/app';
import 'firebase/compat/auth';

const firebaseApp = firebase.initializeApp({ /* ... */);
const firebaseAuth = firebaseApp.auth();

function auth(callback) {

  firebaseAuth.onAuthStateChanged(
    (user) => {
      console.log('onAuthStateChanged:', (user !== null)); // this always logs `false` once, and only once
      if (cb) { cb(user); }
    },
    (error) => {
      console.log('onAuthStateChanged error:', error);
    }
  );

);

Comlink.expose({
  auth
});

/* Browser */

// Set up worker and wrap in Comlink
worker = new Worker(/* ... */);
workerApi = Comlink.wrap(worker);

// Callback as Comlink.proxy
const cb = Comlink.proxy(user => {
  console.log('auth():', user);
});

// Call auth
workerApi.auth(cb);
@rejhgadellaa
Copy link
Author

rejhgadellaa commented Dec 5, 2021

Possibly related: #5370 - except it explicitly mentions that firebase.auth().onAuthStateChanged does work when using the compat version:

In a classic worker, however, it is still possible to use firebase when importing the library from CDN in a non-modular way:

Update, some additional info:

I've tried a couple of older versions (9.5, 9.4.1, 9.2.0, 9.0.2 and 9.0.0) and none of these versions worked as I expect. auth().onAuthStateChanged() is called only once, and always with User = null.

I also tested if the auth().onAuthStateChanged() callback was called correctly in the main thread (browser) by loading the compat auth there and doing the necessary set up - and it does. In the worker, however, still nothing happens.

I noticed that the fetches for getAccountInfo are not executed for the worker, only for the main thread. As soon as I roll back to 8.10.0, I see the request happening for the worker:

https://www.googleapis.com/identitytoolkit/v3/relyingparty/getAccountInfo?key=XYZ

@sam-gc
Copy link
Contributor

sam-gc commented Dec 10, 2021

Thanks for the detail folks. The linked PR should fix the issue and will be available in a release or two. Keep an eye on the changelogs! :)

@rejhgadellaa
Copy link
Author

The fix is live in 9.6.2, thanks!

@firebase firebase locked and limited conversation to collaborators Jan 10, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants