cordova-plugin-firebasex icon indicating copy to clipboard operation
cordova-plugin-firebasex copied to clipboard

FirebasePlugin._onAuthIdTokenChange() fires before device ready, FirebasePlugin undefined

Open globules-io opened this issue 1 year ago • 12 comments
trafficstars

Bug report

FirebasePlugin._onAuthIdTokenChange() is called before device ready, FirebasePlugin undefined at that time. After device ready, FirebasePlugin is defined.

Expected behavior: FirebasePlugin._onAuthIdTokenChange() to start after device is ready

Screenshots Capture Capture2

Environment information Cordova 12.0.0 ([email protected]) [email protected] Windows 10 x64 Pixel 4a with Android 13 Node v20.9.0

Code

    <plugin name="cordova-plugin-device" spec="^2.1.0" />
    <plugin name="cordova-plugin-statusbar" spec="^4.0.0" />
    <plugin name="cordova-plugin-network-information" spec="^3.0.0" />
    <plugin name="cordova-plugin-camera" spec="^7.0.0" />
    <plugin name="cordova-plugin-buildinfo" spec="^4.0.0" />
    <plugin name="cordova-custom-config" spec="^5.1.1" />
    <plugin name="cordova-plugin-androidx-adapter" spec="^1.1.3" />
    <plugin name="cordova-plugin-inappbrowser" spec="^6.0.0" />
    <plugin name="cordova-plugin-geolocation" spec="^5.0.0" />
    <plugin name="cordova-plugin-file" spec="^8.0.1" />
    <plugin name="cordova-plugin-firebasex" spec="^16.5.0">
            <variable name="ANDROID_ICON_ACCENT" value="#742A84" />
            <variable name="IOS_STRIP_DEBUG" value="true" />
            <variable name="FIREBASE_ANALYTICS_COLLECTION_ENABLED" value="false" />
            <variable name="FIREBASE_PERFORMANCE_COLLECTION_ENABLED" value="false" />
            <variable name="FIREBASE_CRASHLYTICS_COLLECTION_ENABLED" value="false" />
    </plugin>

globules-io avatar May 18 '24 09:05 globules-io

I removed all plugins, restarted on a clean project and it works, so it's due to another plugin.

globules-io avatar May 18 '24 14:05 globules-io

Leaving this as information for the next guy, this breaks everything

   <plugin name="cordova-plugin-network-information" spec="^3.0.0" />
   <plugin name="cordova-plugin-inappbrowser" spec="^6.0.0" />
   <plugin name="cordova-plugin-geolocation" spec="^5.0.0" />

globules-io avatar May 18 '24 14:05 globules-io

Reopening hoping someone is facing the same problem. Most of the official plugins break this plugin and this sounds pretty odd to me.

globules-io avatar May 18 '24 17:05 globules-io

Closing again cause it randomly breaks or works. Looks like cordova-android or cordova at large is not stable anymore. I have no idea what's going on.

globules-io avatar May 18 '24 18:05 globules-io

Aaaand reopening again. Firebasex alone with no other plugin works. But then adding other standard plugins break FirebasePlugin. I even uninstalled and reinstalled cordova, restarted the project from scratch and I always end up at the same place.

Would anyone be kind enough to try to build with the following plugins

<plugin name="cordova-plugin-device" spec="^2.1.0" />
<plugin name="cordova-plugin-statusbar" spec="^4.0.0" />
<plugin name="cordova-plugin-camera" spec="^7.0.0" />
<plugin name="cordova-plugin-inappbrowser" spec="^6.0.0" />
<plugin name="cordova-plugin-geolocation" spec="^5.0.0" />    
<plugin name="cordova-plugin-firebasex" spec="^16.5.0">
    <variable name="ANDROID_ICON_ACCENT" value="#742A84" />
    <variable name="IOS_STRIP_DEBUG" value="true" />
    <variable name="FIREBASE_ANALYTICS_COLLECTION_ENABLED" value="false" />
    <variable name="FIREBASE_PERFORMANCE_COLLECTION_ENABLED" value="false" />
    <variable name="FIREBASE_CRASHLYTICS_COLLECTION_ENABLED" value="false" />
</plugin>

This breaks here 100% of the time and give the issue described in the OP.

PS : same thing happens when I add plugins to your test app

globules-io avatar May 18 '24 20:05 globules-io

Have the same error after update plugin to version 16.5.0

webquali avatar May 28 '24 10:05 webquali

Could be related to https://github.com/apache/cordova-android/pull/1605

globules-io avatar May 30 '24 09:05 globules-io

Opened an issue here : https://github.com/apache/cordova-android/issues/1715

globules-io avatar May 30 '24 09:05 globules-io

I could fix this issue by adding a timeout of 2000ms the first time it runs, then 0ms once it has run. This solves the racing issue when having other plugins. Not creating a pull request because this is more of a POC than anything else and not the best in Java, and I don't know the code enough....

 ```
  private static int initTimeout = 2000;

  public static void setTimeout(Runnable runnable, int delay){
         new Thread(() -> {
             try {
                Thread.sleep(delay);
                 runnable.run();
             }
             catch (Exception e){
                 System.err.println(e);
             }
         }).start();
    }


 public void onIdTokenChanged(@NonNull FirebaseAuth firebaseAuth) {
        try {
            FirebaseUser user = firebaseAuth.getCurrentUser();
            user.getIdToken(true).addOnSuccessListener(new OnSuccessListener<GetTokenResult>() {
                @Override
                public void onSuccess(GetTokenResult result) {
                    try {
                        String idToken = result.getToken();
                        if (idToken != null && idToken.equals(instance.currentIdToken)) {
                            return;
                        }
                        instance.currentIdToken = idToken;
                        String providerId = result.getSignInProvider();                            
                        setTimeout(() -> FirebasePlugin.instance.executeGlobalJavascript(JS_GLOBAL_NAMESPACE + "_onAuthIdTokenChange({\"idToken\":\"" + idToken + "\",\"providerId\":\"" + providerId + "\"})"), FirebasePlugin.initTimeout);                            
                    } catch (Exception e) {
                        setTimeout(() -> FirebasePlugin.instance.executeGlobalJavascript(JS_GLOBAL_NAMESPACE + "_onAuthIdTokenChange()"), FirebasePlugin.initTimeout);                          
                    }
                }

            }).addOnFailureListener(new OnFailureListener() {
                @Override
                public void onFailure(@NonNull Exception e) {
                    setTimeout(() -> FirebasePlugin.instance.executeGlobalJavascript(JS_GLOBAL_NAMESPACE + "_onAuthIdTokenChange()"), FirebasePlugin.initTimeout);
                }
            });
        } catch (Exception e) {
            setTimeout(() -> FirebasePlugin.instance.executeGlobalJavascript(JS_GLOBAL_NAMESPACE + "_onAuthIdTokenChange()"), FirebasePlugin.initTimeout);
        }
        FirebasePlugin.initTimeout = 0;
 }

globules-io avatar Jun 02 '24 07:06 globules-io

I have the same problem, but the app and all plugins still work fine after the error. It would be nice if the error was fixed though.

TheNotorius0 avatar Jun 05 '24 10:06 TheNotorius0

Same problem here, causing quite a few ANR's on android, with latest cordova android version 13.0.0

Franjanko avatar Jun 12 '24 14:06 Franjanko

It appears this error only happens on API 33, on first start of the app.
API 34 works just fine, as far as I can tell, by running the same app in Android Studio emulators...

globules-io avatar Jul 14 '24 11:07 globules-io

This should now be fixed in v17.0.0 of the plugin: on both Android & iOS, it now checks the plugin if is initialised (if the plugin JS API has been loaded by Cordova) before attempting to call the JS API. If not initialised when attempting to call the JS API from native, it queues up the calls and invokes them when the plugin has been initialised.

dpa99c avatar Sep 25 '24 11:09 dpa99c

I just upgraded to Cordova@12, [email protected] and [email protected], targeting SDK 34 - and this issue is now happening upon app launch. Nothing else in my code base has changed, simply just upgrading several components has caused this error to happen. I know this was fixed in v17.0.0 - but it appears to be back.

Its not happening on every app launch though, its happening about 50% of the time.

rolinger avatar Jun 30 '25 19:06 rolinger

@globules-io fix here looks like it did the trick: https://github.com/dpa99c/cordova-plugin-firebasex/pull/941 - restarted app about 20 times and the error hasn't triggered once.

rolinger avatar Jul 05 '25 13:07 rolinger

@rolinger I think you mean https://github.com/dpa99c/cordova-plugin-firebasex/pull/942 right ?

globules-io avatar Jul 11 '25 15:07 globules-io

@globules-io - I did mean #941, because that shows the actual config changes needed - I manually edited my files as opposed to doing a new pull.

rolinger avatar Jul 11 '25 16:07 rolinger

@rolinger that won't work, you need #942

globules-io avatar Jul 11 '25 17:07 globules-io