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

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

Closed
globules-io opened this issue May 18, 2024 · 13 comments

Comments

@globules-io
Copy link

globules-io commented May 18, 2024

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
Copy link
Author

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

@globules-io
Copy link
Author

globules-io commented May 18, 2024

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
Copy link
Author

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 globules-io reopened this May 18, 2024
@globules-io
Copy link
Author

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
Copy link
Author

globules-io commented May 18, 2024

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 globules-io reopened this May 18, 2024
@webquali
Copy link

Have the same error after update plugin to version 16.5.0

@globules-io
Copy link
Author

Could be related to apache/cordova-android#1605

@globules-io
Copy link
Author

Opened an issue here : apache/cordova-android#1715

@globules-io
Copy link
Author

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;
 }

@TheNotorius0
Copy link

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.

@Franjanko
Copy link

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

@globules-io
Copy link
Author

globules-io commented Jul 14, 2024

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...

dpa99c pushed a commit that referenced this issue Sep 25, 2024
…prevent calling functions on the plugin JS API before Cordova has loaded the plugin JS namespace.

Relates to #888
dpa99c pushed a commit that referenced this issue Sep 25, 2024
…prevent calling functions on the plugin JS API before Cordova has loaded the plugin JS namespace.

Relates to #888
@dpa99c dpa99c closed this as completed in 7f02206 Sep 25, 2024
@dpa99c
Copy link
Owner

dpa99c commented Sep 25, 2024

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants