diff --git a/android/src/main/kotlin/com/linusu/flutter_web_auth/FlutterWebAuthPlugin.kt b/android/src/main/kotlin/com/linusu/flutter_web_auth/FlutterWebAuthPlugin.kt index 43fd9d2..a8e5103 100644 --- a/android/src/main/kotlin/com/linusu/flutter_web_auth/FlutterWebAuthPlugin.kt +++ b/android/src/main/kotlin/com/linusu/flutter_web_auth/FlutterWebAuthPlugin.kt @@ -28,16 +28,27 @@ class FlutterWebAuthPlugin(private val context: Context): MethodCallHandler { "authenticate" -> { val url = Uri.parse(call.argument("url")) val callbackUrlScheme = call.argument("callbackUrlScheme")!! + val showBrowserChooser = call.argument("showBrowserChooser")!! callbacks[callbackUrlScheme] = resultCallback - val intent = CustomTabsIntent.Builder().build() val keepAliveIntent = Intent(context, KeepAliveService::class.java) - intent.intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP or Intent.FLAG_ACTIVITY_NEW_TASK) - intent.intent.putExtra("android.support.customtabs.extra.KEEP_ALIVE", keepAliveIntent) - - intent.launchUrl(context, url) + if (showBrowserChooser) { + val intent = Intent(Intent.ACTION_VIEW) + intent.data = url + intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP or Intent.FLAG_ACTIVITY_NEW_TASK) + intent.putExtra("android.support.customtabs.extra.KEEP_ALIVE", keepAliveIntent) + // Create intent to show the chooser dialog + val chooser = Intent.createChooser(intent, "") + context.startActivity(chooser) + } else { + val intent = CustomTabsIntent.Builder().build() + intent.intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP or Intent.FLAG_ACTIVITY_NEW_TASK) + intent.intent.putExtra("android.support.customtabs.extra.KEEP_ALIVE", keepAliveIntent) + intent.launchUrl(context, url) + } + } "cleanUpDanglingCalls" -> { callbacks.forEach{ (_, danglingResultCallback) -> diff --git a/lib/flutter_web_auth.dart b/lib/flutter_web_auth.dart index 1265e13..e1145b3 100644 --- a/lib/flutter_web_auth.dart +++ b/lib/flutter_web_auth.dart @@ -26,15 +26,21 @@ class FlutterWebAuth { /// Ask the user to authenticate to the specified web service. /// - /// The page pointed to by [url] will be loaded and displayed to the user. From the page, the user can authenticate herself and grant access to the app. On completion, the service will send a callback URL with an authentication token, and this URL will be result of the returned [Future]. + /// The page pointed to by [url] will be loaded and displayed to the user. + /// From the page, the user can authenticate herself and grant access to the app. + /// On completion, the service will send a callback URL with an authentication token, + /// and this URL will be result of the returned [Future]. /// - /// [callbackUrlScheme] should be a string specifying the scheme of the url that the page will redirect to upon successful authentication. - static Future authenticate({@required String url, @required String callbackUrlScheme}) async { + /// [callbackUrlScheme] a string specifying the scheme of the url that the page will redirect to upon successful authentication. + /// [showBrowserChooser] a boolean to always show a browser chooser to the user when he has multiple browser installed, + /// can be usefull if some browser restriction rules are set by the authentication service). + static Future authenticate({@required String url, @required String callbackUrlScheme, bool showBrowserChooser = false}) async { WidgetsBinding.instance.removeObserver(_resumedObserver); // safety measure so we never add this observer twice WidgetsBinding.instance.addObserver(_resumedObserver); return await _channel.invokeMethod('authenticate', { 'url': url, 'callbackUrlScheme': callbackUrlScheme, + 'showBrowserChooser': showBrowserChooser, }) as String; }