v9 modular: firebase getAuth method throwing error if cookies are disabled by browser (no localStorage) (original) (raw)

[REQUIRED] Describe your environment

[REQUIRED] Describe the problem

When cookies are disabled by the browser (which seems to be the case by default in private browsing for third party website, loaded on iframe for instance, then the getAuth throws an error.
It's the same issue that has been reported here #5274 but the given answer isn't convenient.
I really think that in term of developer experience, it shouldn't be our role to check if the localStorage is available and depending on that, have a custom auth initialization, like I had to do here:

let auth;
export const getAuth = (): Auth => {
  if (auth) {
    return auth;
  }

  const hasAccessToLocalStorage = !!getLocalStorage();
  auth = initializeAuth(getApp(), {
    persistence: hasAccessToLocalStorage ? browserLocalPersistence : indexedDBLocalPersistence,
  });
  return auth;
};

If Firebase isn't handling this case, then the getAuth method is useless because we'll always have to check if the localStorage is available.

Steps to reproduce:

import { getAuth, onAuthStateChanged } from 'firebase/auth';

onAuthStateChanged(getAuth(), (user) => { console.log(user); });

Thank you for your time, and sorry if what I suggest isn't possible