Skip to content

Add browser chooser #41

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -28,16 +28,27 @@ class FlutterWebAuthPlugin(private val context: Context): MethodCallHandler {
"authenticate" -> {
val url = Uri.parse(call.argument("url"))
val callbackUrlScheme = call.argument<String>("callbackUrlScheme")!!
val showBrowserChooser = call.argument<Boolean>("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) ->
Expand Down
12 changes: 9 additions & 3 deletions lib/flutter_web_auth.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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<String> 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<String> 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', <String, dynamic>{
'url': url,
'callbackUrlScheme': callbackUrlScheme,
'showBrowserChooser': showBrowserChooser,
}) as String;
}

Expand Down