meds
meds

Reputation: 22956

getAuth().onAuthStateChanged not called, async not resolving in capacitor?

This may be a firebase issue or a capacitor issue with promises - I'm building an iOS app with capacitor and NextJS.

I'm finding async functions (at least to firebase) aren't completing even when they're successful, for example a call to 'createUserWithEmailAndPassword' from firebase succeeds (the account is created on firebase) however it doesn't resolve,

onClick={() => { console.log("begin sign up"); setLoading(true) try { createUserWithEmailAndPassword(getAuth(), email, password).then(data => {

                }).catch(e => {
                    setError(JSON.stringify(e))
                }).finally(() => setLoading(false))
            }
            catch (e) {
                console.log(JSON.stringify(e));
                setLoading(false);
            }
        }}

This function works fine in a standard nextjs environment so the logic has been validated, making me think it might have to do with async functions in capicitor.

I also seperately have a hook into the callback: getAuth().onAuthStateChanged and getAuth().onIdTokenChanged which are never called.

Update: I used proxyman to inspect network traffic - the sign in request is suceeding with a valid payload returned however on the capacitor side it's not resolving (i.e. calling 'then' or setting a user on the firebase auth).

Upvotes: 2

Views: 936

Answers (1)

RGe
RGe

Reputation: 1859

You seem to have the following problem: https://github.com/robingenz/capacitor-firebase-authentication/issues/78

This was the solution (Source: harryherskowitz.com):

function whichAuth() {
  let auth
  if (Capacitor.isNativePlatform()) {
    auth = initializeAuth(app, {
      persistence: indexedDBLocalPersistence
    })
  } else {
    auth = getAuth()
  }
  return auth
}

export const auth = whichAuth()

You must set indexedDBLocalPersistence as persistence when you initialize auth.

Upvotes: 5

Related Questions