invertase / react-native-apple-authentication

A React Native library providing support for Apple Authentication on iOS and Android.
Other
1.41k stars 224 forks source link

Android crash - Method addObserver must be called on the main thread #195

Closed yuri-lomashko-itechart closed 3 years ago

yuri-lomashko-itechart commented 3 years ago

Hello, I always receive the error when calling the method signIn on Android

Code example:

            if (!appleAuthAndroid.isSupported) {
                throw new Error('Unsupported version of Android');
            }
            try {
                const rawNonce = generateStringUID();
                const state = generateStringUID();
                appleAuthAndroid.configure({
                    clientId: 'http://xxx',
                    redirectUri: 'https://xxx/callback',
                    responseType: appleAuthAndroid.ResponseType.ALL,
                    scope: appleAuthAndroid.Scope.ALL,
                    nonce: rawNonce,
                    state,
                });

                const appleAuthRequestResponse = await appleAuthAndroid.signIn();
                if (appleAuthRequestResponse.state !== state) {
                    throw new Error('Cannot sign in with Apple');
                }
                return {
                    idToken: appleAuthRequestResponse.id_token!,
                    authorizationCode: appleAuthRequestResponse.code,
                    nonce: rawNonce,
                };
            } catch (error) {
                switch (error.message) {
                    case appleAuthAndroid.Error.NOT_CONFIGURED:
                    case appleAuthAndroid.Error.SIGNIN_FAILED:
                        throw new Error('Cannot sign in with Apple');
                    case appleAuthAndroid.Error.SIGNIN_CANCELLED:
                        throw CanceledError;
                }
                throw error;
            }

Error:

8:33 2021-03-12 07:27:07.969 30003-30196/com.tullyapp.test E/unknown:ReactNative: Exception in native call
    java.lang.IllegalStateException: Method addObserver must be called on the main thread
        at androidx.lifecycle.LifecycleRegistry.enforceMainThreadIfNeeded(LifecycleRegistry.java:317)
        at androidx.lifecycle.LifecycleRegistry.addObserver(LifecycleRegistry.java:172)
        at androidx.fragment.app.Fragment.initLifecycle(Fragment.java:496)
        at androidx.fragment.app.Fragment.<init>(Fragment.java:476)
        at androidx.fragment.app.DialogFragment.<init>(DialogFragment.java:138)
        at com.RNAppleAuthentication.webview.SignInWebViewDialogFragment.<init>(SignInWebViewDialogFragment.kt:18)
        at com.RNAppleAuthentication.webview.SignInWebViewDialogFragment$Companion.newInstance(SignInWebViewDialogFragment.kt:24)
        at com.RNAppleAuthentication.SignInWithAppleService.show(SignInWithAppleService.kt:80)
        at com.RNAppleAuthentication.AppleAuthenticationAndroidModule.signIn(AppleAuthenticationAndroidModule.java:232)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.facebook.react.bridge.JavaMethodWrapper.invoke(JavaMethodWrapper.java:372)
        at com.facebook.react.bridge.JavaModuleWrapper.invoke(JavaModuleWrapper.java:151)
        at com.facebook.react.bridge.queue.NativeRunnable.run(Native Method)
        at android.os.Handler.handleCallback(Handler.java:883)
        at android.os.Handler.dispatchMessage(Handler.java:100)
        at com.facebook.react.bridge.queue.MessageQueueThreadHandler.dispatchMessage(MessageQueueThreadHandler.java:27)
        at android.os.Looper.loop(Looper.java:214)
        at com.facebook.react.bridge.queue.MessageQueueThreadImpl$4.run(MessageQueueThreadImpl.java:226)
        at java.lang.Thread.run(Thread.java:919)

[Updated] Possible solution in bridge:

getCurrentActivity().runOnUiThread(
        new Runnable() {
            @Override
            public void run() {
                service.show();
            }
        }
);

Env:

Yaroslav-Syrota commented 3 years ago

I completely agree with this option to fix the problem.

mikehardy commented 3 years ago

That looks like the correct way to fix that stack trace - if you propose a PR I can merge + release @yuri-lomashko-itechart :pray:

mikehardy commented 3 years ago

2.1.1 is out with the fix :rocket:

https://github.com/invertase/react-native-apple-authentication/releases/tag/v2.1.1

yuri-lomashko-itechart commented 3 years ago

Thanks for the quick release 👍

BoTomVK commented 3 years ago

Unfortunately on v2.1.2 still occurs, 2.1.1 works fine

mikehardy commented 3 years ago

@BoTomVK that defies believe - I can't see anything in the v2.1.1 vs v2.1.2 diff that would affect this:

https://github.com/invertase/react-native-apple-authentication/compare/v2.1.1...v2.1.2

That implies a very rare crash (that would happen on v2.1.1 as well but just was not seen), something unrelated that is causing it (outside this module?), or just generally a bad understanding of what's going on.

badaz commented 2 years ago

I get this same crash and Exception (on android, release build only, haven't tested on IOS) after upgrading from 2.1.0 to 2.1.5 (RN 0.64.2), only calling isSupported method on app startup. Reverting to 2.1.1 fixes the problem. I did not have the problem with v2.1.0 though. Maybe this issue should be reopened ?

java.lang.IllegalStateException: Method addObserver must be called on the main thread at androidx.lifecycle.LifecycleRegistry.enforceMainThreadIfNeeded(LifecycleRegistry.java:317) at androidx.lifecycle.LifecycleRegistry.addObserver(LifecycleRegistry.java:172) at io.sentry.android.core.AppLifecycleIntegration.register(AppLifecycleIntegration.java:53) at io.sentry.core.Sentry.init(Sentry.java:159) at io.sentry.core.Sentry.init(Sentry.java:96) at io.sentry.android.core.SentryAndroid.init(SentryAndroid.java:59) at io.sentry.android.core.SentryAndroid.init(SentryAndroid.java:44) at io.sentry.RNSentryModule.startWithOptions(RNSentryModule.java:74) at java.lang.reflect.Method.invoke(Method.java) at com.facebook.react.bridge.JavaMethodWrapper.invoke(JavaMethodWrapper.java:372) at com.facebook.react.bridge.JavaModuleWrapper.invoke(JavaModuleWrapper.java:151) at com.facebook.react.bridge.queue.NativeRunnable.run(NativeRunnable.java) at android.os.Handler.handleCallback(Handler.java:938) at android.os.Handler.dispatchMessage(Handler.java:99) at com.facebook.react.bridge.queue.MessageQueueThreadHandler.dispatchMessage(MessageQueueThreadHandler.java:27) at android.os.Looper.loop(Looper.java:233) at com.facebook.react.bridge.queue.MessageQueueThreadImpl$4.run(MessageQueueThreadImpl.java:226) at java.lang.Thread.run(Thread.java:923)

mikehardy commented 2 years ago

Hi @badaz unfortunately my last comment still stands, even after I just re-read the diff between 2.1.1 and 2.1.2. Nothing in there looks like it would cause that. I don't see our code in that stack trace at all actually, it looks like you crashed in sentry init.

badaz commented 2 years ago

Hi, thanks for your answer. You're right, it looks like it, however the only change between working and not working is the installed version of this lib. I will investigate more and come back to post my findings