auth0 / auth0-flutter

Auth0 SDK for Flutter
https://pub.dev/documentation/auth0_flutter/latest/
Apache License 2.0
61 stars 41 forks source link

Unable to use auth0 in Isolate #233

Closed eliasjtg closed 1 year ago

eliasjtg commented 1 year ago

Checklist

Description

I'm working in a isolate with background location, sending location to backend authenticated with Auth0.

auth0.credentialsManager.credentials()

This show the following exception:

E/MethodChannel#auth0.com/auth0_flutter/credentials_manager( 6251): Failed to handle method call
E/MethodChannel#auth0.com/auth0_flutter/credentials_manager( 6251): kotlin.UninitializedPropertyAccessException: lateinit property activity has not been initialized
E/MethodChannel#auth0.com/auth0_flutter/credentials_manager( 6251):     at com.auth0.auth0_flutter.CredentialsManagerMethodCallHandler.getActivity(CredentialsManagerMethodCallHandler.kt:25)
E/MethodChannel#auth0.com/auth0_flutter/credentials_manager( 6251):     at com.auth0.auth0_flutter.CredentialsManagerMethodCallHandler.onMethodCall(CredentialsManagerMethodCallHandler.kt:36)
E/MethodChannel#auth0.com/auth0_flutter/credentials_manager( 6251):     at io.flutter.plugin.common.MethodChannel$IncomingMethodCallHandler.onMessage(MethodChannel.java:258)
E/MethodChannel#auth0.com/auth0_flutter/credentials_manager( 6251):     at io.flutter.embedding.engine.dart.DartMessenger.invokeHandler(DartMessenger.java:295)
E/MethodChannel#auth0.com/auth0_flutter/credentials_manager( 6251):     at io.flutter.embedding.engine.dart.DartMessenger.lambda$dispatchMessageToQueue$0$io-flutter-embedding-engine-dart-DartMessenger(DartMessenger.java:322)
E/MethodChannel#auth0.com/auth0_flutter/credentials_manager( 6251):     at io.flutter.embedding.engine.dart.DartMessenger$$ExternalSyntheticLambda0.run(Unknown Source:12)
E/MethodChannel#auth0.com/auth0_flutter/credentials_manager( 6251):     at android.os.Handler.handleCallback(Handler.java:907)
E/MethodChannel#auth0.com/auth0_flutter/credentials_manager( 6251):     at android.os.Handler.dispatchMessage(Handler.java:105)
E/MethodChannel#auth0.com/auth0_flutter/credentials_manager( 6251):     at android.os.Looper.loop(Looper.java:216)
E/MethodChannel#auth0.com/auth0_flutter/credentials_manager( 6251):     at android.app.ActivityThread.main(ActivityThread.java:7625)
E/MethodChannel#auth0.com/auth0_flutter/credentials_manager( 6251):     at java.lang.reflect.Method.invoke(Native Method)
E/MethodChannel#auth0.com/auth0_flutter/credentials_manager( 6251):     at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:524)
E/MethodChannel#auth0.com/auth0_flutter/credentials_manager( 6251):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:987)

Reproduction

Install plugin background_locator_2 to run location background isolate Run the app Kill the app and attempt to get environment in isolate

auth0_flutter version

1.1.0

Flutter version

3.7.8

Platform

Android

Platform version(s)

9

stevehobbsdev commented 1 year ago

Thanks for the report @eliasjtg - is this something you can help us look into @poovamraj?

poovamraj commented 1 year ago

@eliasjtg The plugin requires instance of an activity (context actually) to fetch the credentials. Since it is not available in the background, the operation fails.

Will it be possible for you to fetch the credentials before starting the background service and passing it there and using the service from there?

@stevehobbsdev for us to be not dependent on an Activity we have to pass an instance of context from Auth0FlutterPlugin which provides instance to an applicationContext which seem to be used by official Flutter team as mentioned here

poovamraj commented 1 year ago

Hey @eliasjtg, we have implemented a fix for this. Can you checkout this PR in your project - https://github.com/auth0/auth0-flutter/pull/289

poovamraj commented 1 year ago

@eliasjtg since this is fixed in the PR mentioned above we will be closing this issue. The fix will be released in our next release.